Pattern matching problem - Regex!

Posted in Help the coder! on Apr 24, 2009 at 08:40 IST (about 1 year ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 4 of total 4 on page 1 of 1
Tagged pattern, regex
Post reply
« Previous1Next »

goobimama
Rank: 214

I have this file and i need help on it

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

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

Posted by goobimama on Friday, April 24, 2009, 8:40 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

wrcfan
Rank: 309

^(52245|52246)$

Posted by wrcfan on Friday, April 24, 2009, 10:47 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

anthropo...
Rank: 380

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!

Posted by anthropology on Friday, April 24, 2009, 1:57 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

kevingeek
Rank: 261
^"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)"$.

Posted by kevingeek on Friday, April 24, 2009, 7:21 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.117 seconds