Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   Regex help (http://www.programmingforums.org/showthread.php?t=14540)

titaniumdecoy Nov 20th, 2007 4:03 PM

Regex help
 
Hi all,

I need some help with a regular expression. I need to match entire Python functions; so, beginning with "(\W+)def .*(.*):" I need to math the following line, assuming it begins with more whitespace the first line, and any following lines that begin with the same amount of whitespace as that line. I am not worried about one-liners and other details right now, just what I described.

Please help! I'm not even sure this is possible, but I hope it is.

The Dark Nov 20th, 2007 7:41 PM

Re: Regex help
 
Try something like:
r"^\W*def .*\(.*\):.*\n(\W+).*(\n\1.*)*"
Notes:
  1. I used \r so the backslashes don't mean escape. I don't
  2. Changed the + to a * after the first \W so you can have "def" starting at the start of the line
  3. Changed the ( to \( so that it actually matched the bracket in the source rather than being a collecting group
  4. I used a collecting group (\W+) to collect the first lot of leading spaces
  5. \1 matches the white space collected in the first group above
Hope this helps - It seemed to work for me, but I didn't try it out extensively.


All times are GMT -5. The time now is 3:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC