tags:

views:

184

answers:

3

Take the following contents of a file:

"52245"
"528"
"06156903"
"52246"
"530"
"00584709"

What pattern would match both 52245 and 52246 but nothing else?

+11  A: 

^(52245|52246)$

Brian Knoblauch
Technically correct. I'm almost sure he's looking for something else, but wasn't specific about what.
Max Schmeling
You forgot the quotation marks.
Michael Myers
+1 for making me laugh
Tomalak
@mmyers Oops, didn't see the "grep" tag. I supplied a java regex where quoties would not be used.
Brian Knoblauch
+15  A: 

Something that can only match those two numbers and nothing else:

^\"5224[56]\"$

Now if you're looking for something a bit more general (for example, any number with 5 digits), you'll want something like

^\"\d{5}\"$

I'm assuming the quotation marks (") are part of the file. If they aren't, omit the \" parts from the expression.

The particular grep expression you want is this:

grep -E "^\"[[:digit:]]{5}\"$" filename

or to take a suggestion from the comments:

grep -P "^\"\d{5}\"$" filename

I've tested both and they work on my machine!

Welbog
Yes the quotation marks are part of the file. I am using grep like this:grep ^\"\d{5}\"$ myfileIt returns nothing, I have also tried with -G and -E optionsAny ideas what is wrong?
\d is a Perl style special character. Try grep -P ^\"\d{5}\"$ . I think there are other ways to match digits without -P and \d, but they're not worth it.
David Berger
Take care to escape everything from your shell though. In bash, that means using single quotes around the regexp.
David Schmitt
i'm not sure that the \d{} is correct...I use [[:digit:]]\{5\}..and you should put " around your expression (use grep with -e too).
LB
I've included your suggestion into the answer, David Berger. Good tip. I didn't know about -P until now.
Welbog
+7  A: 
^"5224[56]"$

^"5224(5|6)"$

^"52{2}4[56]"$

^"(52245|52246)"$

...

You should base the regex you use on the semantic you want to express. If you are looking for two arbitrary numbers use ^"(52245|52246)"$. If the numbers have any meaning - a type code or something like that - I would stick with ^"5224(5|6)"$.

Daniel Brückner