View Full Version : RE help, matching strings ending with ";"

June 15th, 2009, 05:58 PM
I'm using python & i am trying to match ";" in a line:

for page in css:
f = open(page, "r")
lines = f.readlines()
count = 1
obcount = 0
ebcount = 0
probwithfile = False
for line in lines:
if not re.match("^{$", line) or not re.match("^}$", line):
if not re.match(".*?;", line):
print "hi"

it prints out "hi" for every line

June 15th, 2009, 06:11 PM
escape the ;

like so:

June 15th, 2009, 06:16 PM
Thanks, it is now this:

if not re.match(".*?\;.*", line):
print "hi"

but that still doesn't work

June 15th, 2009, 11:40 PM
What about:


June 15th, 2009, 11:47 PM
Why are you trying to parse CSS?

June 16th, 2009, 12:54 AM
if you want to match anywhere in the line, use "in" operator

if ";" in line:

if want to match at the end, use endswith(), at the start, use startwith()
No need to use regular expression for such a simple task

June 16th, 2009, 03:43 PM
I've tried

if not line.endswith(";"):
print "hi"


if not ";" in line:
print "hi"

both don't work,
i am wanting to match things like:


June 16th, 2009, 03:46 PM
maybe use line.strip().endswith(';')

June 16th, 2009, 03:49 PM
Thanks, it worked

June 16th, 2009, 04:10 PM
Sorry, no it doesn't!
I'm not back to where I started:

for page in css:
f = open(page, "r")
lines = f.readlines()
count = 1
obcount = 0
ebcount = 0
scount = 0
probwithfile = False
for line in lines:
for match in re.finditer("}", line):
ebcount += 1
for match in re.finditer("{", line):
obcount += 1
if not re.match("[\.a-zA-Z0-9=#:\s_,]*\}.*", line):
if not re.match("[a-zA-Z0-9_=#:\s\.,]*\{.*",line):
print "hi"
if not line.strip().endswith(";"):
print "#################### CSS Error ###################"
print "Line does not end with ';' in file %s on line %i" % (page, count)
print line
scount += 1
probwithfile = True

it doesn't print "hi"

June 16th, 2009, 05:01 PM
You have an indentation error 'probwithfile = False' and also I think you are overcomplicating this, as others have said if you want to match the end of a string use
if string.endswith(var):

You are doing a lot of stuff before you even get anywhere near a print statement, try and add some print statements at each stage to see what exactly is going on.

Also, you might think about posting the rest of the code if your still having issues.



June 16th, 2009, 05:08 PM
i got it working