Skip to content Skip to sidebar Skip to footer

Python Regex To Avoid A Character Earlier In The String

I'd like to use a regex to find an exact string, but not if it's part of a comment, as designated by //. So for example, in the string: hello apple apples // eat an apple It shou

Solution 1:

this pattern will catch what you want in the first sub-pattern

\/\/.*|\b(apple)\b

Demo

Solution 2:

I think you just need to escape your comment for the lookbehind assertion;

    (?<!\/\/)\b(apple)\b ## doesn't work, don't use this.

Try it -- regex101.com

Post a Comment for "Python Regex To Avoid A Character Earlier In The String"