[Tutor] Reg. Expressions Parenthesis

Chris Kavanagh ckava1 at msn.com
Tue Jan 17 09:07:51 CET 2012


Hey guys, girls, hope everyone is doing well.

Here's my question, when using Regular Expressions, the docs say when 
using parenthesis, it "captures" the data. This has got me confused 
(doesn't take much), can someone explain this to me, please??

Here's an example to use. It's kinda long, so, if you'd rather provide 
your own shorter ex, that'd be fine. Thanks for any help as always.


From: [\w\s]+?<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>


     From: matches the literal text "From: "
     [\w\s]+? matches one or more consecutive word characters or space 
characters. The question mark makes the match non-greedy, so it will 
match as few characters as possible while still allowing the whole 
regular expression to match (in this case, it's probably not necessary, 
but it does make the match more efficient since the thing that comes 
immediately afterwards is not a word character or space character).
     < matches a literal less-than sign (opening angle bracket)
     The same regular expression you had before (without From: and 
without parenthesis) is now surrounded by parentheses. This makes it a 
capturing group, so you can call m.group(1) to get the text matched by 
that part of the regex.
     > matches a literal greater-than sign

Thanks,
Chris Kavanagh




More information about the Tutor mailing list