Regular Expression to Match String Exactly?

I'll preface this question by mentioning that while I'm far from a regular expressions guru, they are not completely foreign to me. Building a regular expression to search for a pattern inside a particular string generally isn't a problem for me, but I have a (maybe?) unique situation.

I have a set of values, say:

028938
DEF567987
390987.456
GHI345928.039

I want to match a certain set of strings, such as:

  • Strings composed of exactly 6 digits
  • Strings composed of exactly 6 digits, a decimal, followed by exactly 3 more digits

In the above examples, the first and third values should be matched.

I'm using the regular expressions:

[0-9]{6}
[0-9]{6}.[0-9]{3}

Unfortunately, since all the above examples contain the specified pattern, all values are matched. This is not my intention.

So my question, in a nutshell, is how to write a regular expression that matches a string exactly and completely, with no additional characters to the right or left of the matched pattern? Is there a term for this type of matching? (Google was no help.) TIA


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})?$

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

^\d{6}$
^\d{6}\.\d{3}$

are the correct patterns you can test them 6 digits only and 6 digits dot 3 digits.

^\d{6}((\.\d{3}$)|$)

will match either 6 digits or 6 digits dot 3 digits

Rubular is your friend!


Match this regex:

"^\d{6}((\.\d{3}$)|$)"

i think you want something like this:

"^\d{6}(\.\d{3})?$"

you need to escape the "dot" as it is "any" character in regexp.