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:
		[B]if not re.match("^{$", line) or not re.match("^}$", line):
		        if not re.match(".*?;", line):
                                print "hi"[/B]

it prints out "hi" for every line

Recommended Answers

All 7 Replies

If I understand what you want, you want to use
if ";" in line:
if not "X" in line:
etc.

Reminds me of the time I wrote a barebones css parser for myself. Good times.

Not sure what exactly what you're going at, but try:

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.search("^{$", line) or not re.search("^}$", line):
		        if not re.search(".*;$", line):
                                print "hi"

Thanks, but none of the solutions work!?

if not line.endswith(";"):

[B]or[/B]

if not ";" in line:

Excuse me but why use regex anyways:

>>> lines=['this ends with;','but this ends with:']
>>> print [ x for x in lines if x.endswith(';')]
['this ends with;']
>>>

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" or anything else!

If you had stated what you were trying to do in the beginning, you would probably have a solution by now. First, are you trying to count the "{" and "}" characters? If so, try the code below and after that is working state what you want to do next. Whatever it is, you don't want it inside a
for match in re.finditer("{", line):
statement as that will perform the same re multiple times (once for every "{" found). Whoever supplied the re statements was either playing a joke or trying to prove how much smarter they were than you. Neither solves your problem, and there are other ways that will work, and that you will also understand. There is a programmer saying that goes something like, "Someone has a problem. They say 'I'll solve this with a regular expression.' Now they have two problems." If this doesn't do the count the way you want, post back.

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:
                count = line.count("{")
                obcount += count
                ebcount += line.count( "}" )
        print "total '{' = %d, and total '}' = %d" % (obcount, ebcount)


What?
Did you read the post title or not?
The matching of '{' and '}' is absolutely fine in every way, it works!
I was trying to see if a line ended with ';', I have done this on my own, after trying many things.

smarter they were than you

I did it, & it works perfectly well in my case thanks, it might be better to do it different in other cases, but NOT MINE

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.