NYCPHP Meetup

NYPHP.org

[nycphp-talk] [OT] grep assistance

Paul Houle paul at devonianfarm.com
Tue Sep 26 07:57:27 EDT 2006


Peter Sawczynec wrote:
> Four grep regular expressions I could use help with...
    This is fun.  But it's certainly not a "best practice" way of doing 
things.  (Ask somebody to explain what the #4 regex does...)  I'll take 
a crack at it with PCRE's,  but it's very likely that some of them have 
mistakes on boundary cases.

    If you're working on the UNIX command line,  the easy way to do this 
is with awk.  If you're handling space separated data,  say

775 some data
776 other data

    you can write something like

awk '{if ($1<550} print}'

    awk defaults to space as a separator,  but you can change this with 
the -F option,  say -F: or -F,.  Somebody can look at that command and 
have a pretty clear idea of what it does and a decent chance of doing 
the right thing if 550 changes to 549.

    Note there is a bit of a cheat below in that I'm not considering 
proper fielding...  If you want to match any numbers embedded anywhere 
in the document,  I think you can change ^ -> (^|[^0-9]) and $ -> 
([^0-9]|$).  If you know numbers won't be at the beginning or end of a 
line and will be delimited by spaces,  you can replace ^ and $ with spaces.
>  
> 1) match all id numbers between 100 and 199

/^1[0-9]{2}$/
> 2) match all id numbers less than 550
/^4[0-9]{2}|5[0-4][0-9]$/
> 3) match all id numbers greater than 550
/^[0-9]{4,}|[6-9][0-9]{2}|55[1-9]$/
> 4) match all id numbers from 0-550, but not 400-480
>  
/^[0-3][0-9]{2}|48[1-9]|49[0-9]|5[0-4][0-9]$/



More information about the talk mailing list