regular expressions questions

Newhard, Nick nnewhard at origin.ea.com
Mon Mar 20 18:09:28 CET 2000


I have started working with regular expressions (using the re.py module) for
parsing text input and have a few questions.

1. Single quote detection 

myrule = re.compile(r"^/('|say)(?:[\t ]*$|[\t ]+(.*))")

s1 = "/say Hello!"
s2 = "/' Hello!"

String s1 matches and returns a group ('say', 'Hello!') but string s2 does
not match. Any ideas?  I tried variations with the single quote (\', etc.)
without success.

The command list I use in my rule ('|say) is a lot larger. Any comments on a
better way to set up this rule?

2. Groups and patterns

s2 = "aabbabaaabbb"

If I have a large string containing a random collection of patterns, in this
case a's and b's, how would I for a rule to return groups of them?

For this case, I would want ('aa', 'bb', 'a', 'b', 'aaa', 'bbb')

2a. What if I want to collect whitespace with the a's in the groups too?

s2 = "aa bb a b aaa bbb"

For this case, I would want ('aa ', 'bb', ' a ', 'b', ' aaa ', 'bbb')

Thanks for your help!!

(P.S.  I have "Mastering Regular Expressions" on order but it will be days
before I receive it. *sigh*)




More information about the Python-list mailing list