[sf-lug] regex: how to match any one to four character word in a file

Eric Walstad eric at ericwalstad.com
Wed Dec 10 09:27:38 PST 2008


On Wed, Dec 10, 2008 at 8:37 AM, jim <jim at well.com> wrote:
>
> i've given up on the online tutorials.
Sometimes I have to give up on regex and go with what I'm more
proficient at.  So, while I'm not answering your question in terms of
regex, here's something that might work for you in Python and can be
used in a command pipeline.  If nothing else, it's a simple example of
how to handle pipelining.  I suspect this is nothing new to you but
I'm hoping it's helpful to other listeners...


----- words.py -----
#!/usr/bin/python

import sys


for line in sys.stdin:
    for field in line.split():
        if len(field) > 0 and len(field) <= 4:
            print field


-----------
ewalstad at sawarna:~$ cat foo

a
and
apple
fact
zounds
can do wrds on one line too
ewalstad at sawarna:~$ cat foo | ./words.py
a
and
fact
can
do
wrds
on
one
line
too
ewalstad at sawarna:~$ cat foo | ./words.py | grep "^a.*"
a
and




More information about the sf-lug mailing list