PDA

View Full Version : Regular expressions


GnuVince
10-30-2002, 01:46 PM
Hi guys, could anybody tell me what I should learn about regular expressions now? I'm pretty comfortable with basic-intermediate stuff (I'll explain what I know later), and I would be interested in more advanced features of regular expressions. I know that a couple of guys here are quite guru with them, so tell me please.

I know about:
- repetition (+, *, {n}, {n,} and {n,m})
- character substitution (., \d, \s. \b, [abc], [:alpha:], etc.)
- Grouping

Thanks guys.

Strike
10-30-2002, 02:02 PM
Match groups :)

Example in Python:

>>> foo = re.compile("(?P<first_char>.)\s+(?P<first_number>\d)\s+(?P<the_rest>.*)")
foo
>>> match_obj = foo.match("a 1 blahblahblah")
>>> match_obj.groups()
('a', '1', 'blahblahblah')
>>> match_obj.group("first_char")
'a'
>>> match_obj.group("first_number")
'1'
>>> match_obj.group("the_rest")
'blahblahblah'
>>> match_obj = foo.match("b 2 ")
>>> match_obj.group("first_char")
'b'
>>> match_obj.group("first_number")
'2'
>>> match_obj.group("the_rest")
''


Basically, within the regex, you can assign a sort of "key" to a given grouped regex. It can be used in substitution or simply extracting data. I think they're cool :)

GnuVince
10-30-2002, 03:56 PM
Nice! Thanks!!

kmj
10-30-2002, 05:09 PM
Another important concept: greedy vs. non-greedy matches


>>> bar = re.compile("a.*b")
>>> baz = re.compile("a.*?b")
>>> blarg = "a baby bib"
>>> m1 = bar.match(blarg)
>>> (m1.start(), m1.end())
(0, 10)
>>> m2 = baz.match(blarg)
>>> (m2.start(), m2.end())
(0, 3)



Notice the effect of the '?' after the *. By default, * is "greedy", meaning it will match as much as possible. The '?" makes it non-greedy, causing it to match as little as possible instead. This little thing can trip you up if you're not careful.

For more info on regex's... (It's in python, but the concepts are probably applicable in most other places

http://py-howto.sourceforge.net/regex/regex.html

Also, reading the library reference on the 're' module can be quite helpful.

LonelyKing
11-04-2002, 05:58 AM
If you really want to get into regex, I really suggest Mastering Regular Expressions 2nd Edition by Friedl from O'Reilly... it's pretty inexpensive and it's really a great tool for regex...