use ^ and $ to match the start and end of your string
^[0-9]{6}$
^[0-9]{6}\.[0-9]{3}$
Reference: http://www.regular-expressions.info/anchors.html
Also, as noted by Mikael Svenson, you can use the word boundary \b if you are searching for this pattern in a larger chunk of text.
Reference: http://www.regular-expressions.info/wordboundaries.html
You could also write both those regexes in one shot
^\d{6}(\.\d{3})?$
Answer from CaffGeek on Stack Overflowuse ^ and $ to match the start and end of your string
^[0-9]{6}$
^[0-9]{6}\.[0-9]{3}$
Reference: http://www.regular-expressions.info/anchors.html
Also, as noted by Mikael Svenson, you can use the word boundary \b if you are searching for this pattern in a larger chunk of text.
Reference: http://www.regular-expressions.info/wordboundaries.html
You could also write both those regexes in one shot
^\d{6}(\.\d{3})?$
You can use ^ to require the matching at the start of a line and $ to require the end of a line
^[0-9]{6}\.[0-9]{3}$
[0-9] can also be written as \d
^\d{6}\.\d{3}$
You can also use \b for word boundaries if you want to match your pattern in a line with eg. spaces in them
\btest\b
will match the word test in this line
this is a test for matching
I suggest bookmarking the MSDN Regular Expression Quick Reference
you want to achieve a case insensitive match for the word "rocket" surrounded by non-alphanumeric characters. A regex that would work would be:
\W*((?i)rocket(?-i))\W*
What it will do is look for zero or more (*) non-alphanumeric (\W) characters, followed by a case insensitive version of rocket ( (?i)rocket(?-i) ), followed again by zero or more (*) non-alphanumeric characters (\W). The extra parentheses around the rocket-matching term assigns the match to a separate group. The word rocket will thus be in match group 1.
UPDATE 1:
Matt said in the comment that this regex is to be used in python. Python has a slightly different syntax. To achieve the same result in python, use this regex and pass the re.IGNORECASE option to the compile or match function.
\W*(rocket)\W*
On Regex101 this can be simulated by entering "i" in the textbox next to the regex input.
UPDATE 2 Ismael has mentioned, that the regex is not quite correct, as it might match "1rocket1". He posted a much better solution, namely
(?:^|\W)rocket(?:$|\W)
I think the look-aheads are overkill in this case, and you would be better off using word boundaries with the ignorecase option,
\brocket\b
In other words, in python:
>>> x="rocket's"
>>> y="rocket1."
>>> c=re.compile(r"\brocket\b",re.I) # with the ignorecase option
>>> c.findall(y)
[]
>>> c.findall(x)
['rocket']
RegEx that does NOT match desired pattern
Regex to test if strings exist in a string to match
Match multiple strings
Regex match anything but
Videos
Try the following regular expression:
Copy^Red October$
By default, regular expressions are case sensitive. The ^ marks the start of the matching text and $ the end.
Generally, and with default settings, ^ and $ anchors are a good way of ensuring that a regex matches an entire string.
A few caveats, though:
If you have alternation in your regex, be sure to enclose your regex in a non-capturing group before surrounding it with ^ and $:
Copy^foo|bar$
is of course different from
Copy^(?:foo|bar)$
Also, ^ and $ can take on a different meaning (start/end of line instead of start/end of string) if certain options are set. In text editors that support regular expressions, this is usually the default behaviour. In some languages, especially Ruby, this behaviour cannot even be switched off.
Therefore there is another set of anchors that are guaranteed to only match at the start/end of the entire string:
\A matches at the start of the string.
\Z matches at the end of the string or before a final line break.
\z matches at the very end of the string.
But not all languages support these anchors, most notably JavaScript.
Hi, I’m looking for a regular expression to match multiple strings (including a literal asterisk) So far this is what I have got: https://regex101.com/r/bmRaaT/1
Unfortunately it’s not accepting the “*”
Hello all.
Can anyone help me with the below request?
I need a regex condition that matches anything EXCEPT the specified strings. I have tried the following regex, but unfortunately it does not match if I have a string that contains one of the values listed below, e.g. "blue dragon" does not match because it contains "blue". I need it to match if it isn't exactly the values I have in the regex, so if I have "blue dragon" it matches still, although I have "blue" in the regex.
Thanks in advance!
^(?!.*(red|green|blue))