tags:

views:

105

answers:

7

What is the regular expression to match FS00000 were the 0s can be a number from 0-9. There can only be 5 numbers following the FS.

+8  A: 

Try

FS\d{5}

\d means any digit, and {5} means exactly 5 of them.

Michael Myers
shouldn't you add something like \D to prevent a sixth digit?
tanascius
\d will match any Unicode Decimal Digit (http://www.fileformat.info/info/unicode/category/Nd/list.htm) if used in .NET
VVS
This is the most basic form, and yes, it could be improved. Like for instance, I could have surrounded it with ^$ -- but the question was so vague that I didn't want to add possibly extraneous elements.
Michael Myers
you are right - the question can be interpreted as "six digits are not possible"
tanascius
+9  A: 
^FS\d{5}$

which matches the line start (^ - you may not need this), then FS, then a digit \d 5 times {5}, then the line end $ (again, you may not need this, but you'd then have to protect against a sixth digit following).

You don't specify which language/regexp, but the above is pretty generic.

EDIT: You can provide word boundary markers instead of line start/end markers, which is a little more generic. \b will mark a word boundary (in Perl - see Perlre - but equivalents exist in other languages)

Brian Agnew
Cheers I forgot the $ :(
How about adding word boundaries instead of the start-of-line and end-of-line markers?
Huppie
Yes - I think would be useful. I'll modify to suggest this
Brian Agnew
... or you could upvote my answer ;-)
Huppie
A shameless attempt at votemongering! But that would make sense. Done
Brian Agnew
For Perl, you want \b instead of \w and \W (which match word or non-word characters, not a boundary). Check the reference you linked to. :)
iammichael
@ianmichael - You're right. Too hasty, I fear
Brian Agnew
+1  A: 

FS[0-9]{5}

John Barrett
+1  A: 

Only 5 numbers means 0-5 numbers?

FS[0-9]\{1,5\}
nik
A: 
^FS[0-9]{5}$

will do the trick.

This matches any string that begins (^) with FS followed by 5 times 0-9 and then ends ($).

Don't use \d in this case since it will also match Unicode Decimal Digits (at least in .NET).

VVS
+6  A: 

If the exact word should be matched don't forget word boundaries:

\bFS\d{5}\b

Depending on the language chosen the syntax for a word boundary might differ.

Huppie
Word boundaries look to be a good move here
Brian Agnew
+1  A: 

Note, that if FS00000 is part of other text and doesn't occupy the entire line, you should surround the FS\d{5} pattern by word boundaries rather than line boundaries:

\bFS\d{5}\b
Helen