tags:

views:

53

answers:

3
+1  Q: 

Regex Do Not Match

I can't figure out how to match comments but not html hex in regex. For example I want the script to match

#I'm a comment, yes I am

but not

#FF33AF
+2  A: 

You could use negative lookahead. From the python documentation:

(?!...)

Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.

Zitrax
Perfect! I have found this bit of code before but all documentation of it I found were horrible examples. Thanks!
c00lryguy
+1  A: 

Well, the obvious regex is going to be something like:

(?m-:^\s*#(?![0-9A-Fa-f]{6}).*$)

This gives you all lines that start with a '#'. From your post your not very specific but I think that is what your looking for.

Updated:

Corrected to only allow the six:

(?m-:^\s*#(?![0-9A-Za-z]{6}\s*$).*$)
csharptest.net
+2  A: 

To do the job right you need a parser not a regular expression matcher. For example, is "#decade" a comment or a color name? You can't know without a little context.

Bryan Oakley