[CentralOH] Regex Question

Eric Floehr eric at intellovations.com
Tue Sep 30 15:34:45 CEST 2014


On Mon, Sep 29, 2014 at 10:46 AM, Joshua Kramer <joskra42.list at gmail.com>
wrote:

> Hello All,
>
> I have an interesting challenge with a regex.  I need to match any line
> that:
>     Does not contain a !  or a # as the first non-whitespace character
>     Contains the word 'if', but not the string "-if" or the string "ifdef"
>

I'm not a regex master, and maybe the goal is to do this in a single line.
If that's the case, I don't know how to do that.

But if I were solving the problem, I would do it in two steps:

1. Is this a comment line (a line whose first non-whitespace character is !
or #)?
2. If so, stop
3. If not, does it contain the word "if"?

found_if_statement = false
if not re.match("^\s*[!#]", string):
    found_if_statement = re.match("[^-]if\w", string)

if found_if_statement:
    ...do stuff...

A couple of other notes...

^\s*[^!#].*[^-]if[^a-zA-Z0-9_].*
>

[^a-zA-Z0-9_] can be simplified to \w

Also, that "match anything except" means you still require something after
the if, which might be ok for your purposes. So a line the end with "if"
won't be matched. You could use \b if you want that. But both your
original, \w, and \b all will match "if.", "if)", "if]", etc. which again
may be ok.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20140930/0a8554ce/attachment.html>


More information about the CentralOH mailing list