Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regular Expressions in Python

Reply
Thread Tools

Regular Expressions in Python

 
 
fossil_blue
Guest
Posts: n/a
 
      03-01-2004
Dear Gurus,

I am trying to find out how to write an effective regular expression
in python for the following scenario:

"any number of leading spaces at the beginning of a line" "follow
by a string" "there maybe a string that starts with *"

for example:

END *This is a comment

but I don't want to match this:

END e * This is a line with an error (e)

thanks,
Noel
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      03-01-2004
opt_spaces = " *"
identifier = "[A-Za-z_][A-Za-z0-9_]+"
comment = "\*.*"
opt_comment = "(%s)?" % comment

pat = re.compile(opt_spaces + identifier + opt_spaces + opt_comment + "$")

for test in (
" END *This is a comment",
" END e * This is a line with an error (e)"):
print test, pat.match(test)

Jeff

 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      03-01-2004
"Jeff Epler" <> wrote in message
news:mailman.16.1078152536.22701.python-...
> opt_spaces = " *"
> identifier = "[A-Za-z_][A-Za-z0-9_]+"
> comment = "\*.*"
> opt_comment = "(%s)?" % comment
>
> pat = re.compile(opt_spaces + identifier + opt_spaces + opt_comment + "$")
>
> for test in (
> " END *This is a comment",
> " END e * This is a line with an error (e)"):
> print test, pat.match(test)
>
> Jeff
>

Assuming you're more interested in the identifier than in the comment,
change identifier to "([A-Za-z_][A-Za-z0-9_]+)" so that the keyword gets
saved in the pat.match.groups() list.

-- Paul


 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      03-01-2004
"fossil_blue" <> wrote in message
news: om...
> Dear Gurus,
>
> I am trying to find out how to write an effective regular expression
> in python for the following scenario:
>
> "any number of leading spaces at the beginning of a line" "follow
> by a string" "there maybe a string that starts with *"
>
> for example:
>
> END *This is a comment
>
> but I don't want to match this:
>
> END e * This is a line with an error (e)
>
> thanks,
> Noel


Here's an example with sample code using both re's and pyparsing. Note that
the single .ignore() call takes care of ignoring comments on all contained
grammar constructs, and non-significant whitespace is implicitly ignored (so
no need to litter your matching expressions with lots of opt_spaces-type
content).

-- Paul
========================
from pyparsing import Word, alphas, alphanums, restOfLine, LineEnd,
ParseException

testdata = """
END *This is a comment
END*This is a comment (but the next line has no comment)
END
END e * This is a line with an error (e)"""
enquote = lambda st : ( '"%s"' % st )

print "test with pyparsing"
grammar = Word( alphas, alphanums ).setName("keyword") + LineEnd()
comment = "*" + restOfLine
grammar.ignore( comment )

for test in testdata.split("\n"):
try:
print enquote(test),"\n->",
print grammar.parseString( test )
except ParseException, pe:
print pe

print

import re
print "test with re"
opt_spaces = " *"
#identifier = "[A-Za-z_][A-Za-z0-9_]+" - I'm guessing this regexp should
have ()'s for accessing content as a group
identifier = "([A-Za-z_][A-Za-z0-9_]+)"
comment = "\*.*"
opt_comment = "(%s)?" % comment

pat = re.compile(opt_spaces + identifier + opt_spaces + opt_comment + "$")

for test in testdata.split("\n"):
print enquote(test),"\n->",
if pat.match(test):
print pat.match(test).groups()
else:
print "Bad text"

========================
Gives this output:

test with pyparsing
""
-> Expected keyword (0), (1,1)
"END *This is a comment"
-> ['END']
" END*This is a comment (but the next line has no comment)"
-> ['END']
" END"
-> ['END']
" END e * This is a line with an error (e)"
-> Expected end of line (, (1,9)

test with re
""
-> Bad text
"END *This is a comment"
-> ('END', '*This is a comment')
" END*This is a comment (but the next line has no comment)"
-> ('END', '*This is a comment (but the next line has no comment)')
" END"
-> ('END', None)
" END e * This is a line with an error (e)"
-> Bad text


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python regular expressions just ain't PCRE Wiseman Python 13 05-08-2007 10:03 PM
Python Regular Expressions: re.sub(regex, replacement, subject) Vibha Tripathi Python 3 07-05-2005 07:43 PM
Regular Expressions - Python vs Perl codecraig Python 30 04-26-2005 05:17 AM
Looking for help with regular expressions- not Python Specific Tony C Python 6 01-09-2004 08:39 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57