Reputation: 9752
I'm trying to write a regular expression match
I'd like to match c99 in files, so long as its not part of a hexadecimal color code for example
#000c99
/mysite.com/c99.php
%20c99.php
c99?hi=moo
Is this even possible with regex?
Upvotes: 1
Views: 127
Reputation: 251408
Using the regex module:
>>> rx = regex.compile(r"(?<!#\d{0,3})c99")
>>> rx.findall("#000c99")
[]
>>> rx.findall("/mysite.com/c99.php")
[u'c99']
>>> rx.findall("%20c99.php")
[u'c99']
>>> rx.findall("c99?hi=moo")
[u'c99']
Upvotes: 2
Reputation:
The most straight forward way would be to match lines with "c99" in them then discard any where the c99 is in a color code:
line = fileHandle.readline()
while (line) :
if (re.search("c99", line)) :
if (re.search("#.?.?.?c99", line)) :
pass
else :
# line contains c99 not in a color code
line = fileHandle.readline()
There's probably a way to do it within a single regex, but this was just the first thing that came to mind.
Upvotes: 1