Asking the experts for a less greedy regex

25 views
Skip to first unread message

F. Alfredo Rego

unread,
Jun 22, 2019, 1:03:25 PM6/22/19
to BBEdit Talk
Hello Regex gurus,

I’m interested in finding “if” statements that contain the negation operator “!” (or potentially several negation operators) anywhere, regardless of blank lines, parentheses, indentation, and so on.

My motivation is to verify, one by one, the scope(s) of the negation(s).

For instance, considere this code snippet:

                 if ( ID ==   OK_ID )    OK_ID_procedure (ID);
            else if ( ID == fake_ID )  fake_ID_procedure (ID);
            else if ( ID ==  old_ID )   old_ID_procedure (ID);
            else if ( ID != lost_ID ) check_ID_procedure (ID);


I would like to select JUST THE LAST “if” statement, which is the only one that contains the negation operator, like so:




The best I have done, so far, is this regex, with a space as its first character to ignore #ifdef and such: 
 if(?s).+?\(.*?!.*?(;|\{)

It works well but it produces too many “false positives” through which I need to wade. 

This regex selects everything from the first “if” in this snippet to the last semicolon:



No big deal, but I thought I would ask the experts for a less greedy regex. 

I tried several possible non-greedy options but they produced unexpected/undesirable secondary effects.

Thanks for any hints.

Alfredo

Patrick Woolsey

unread,
Jun 22, 2019, 3:15:39 PM6/22/19
to bbe...@googlegroups.com
On Jun 22, 2019, at 13:03, F. Alfredo Rego <F.Alfr...@gmail.com> wrote:
>
> I’m interested in finding “if” statements that contain the negation operator “!” (or potentially several negation operators) anywhere, regardless of blank lines, parentheses, indentation, and so on.
>
> My motivation is to verify, one by one, the scope(s) of the negation(s).
>
> For instance, considere this code snippet:
>
> if ( ID == OK_ID ) OK_ID_procedure (ID);
> else if ( ID == fake_ID ) fake_ID_procedure (ID);
> else if ( ID == old_ID ) old_ID_procedure (ID);
> else if ( ID != lost_ID ) check_ID_procedure (ID);
>
>
> I would like to select JUST THE LAST “if” statement, which is the only one that contains the negation operator, like so:
>

Based on your description above, I expect something like this should do the job, or at least serve as a starting point for further experimentation :-):

Search for: (( |)if \(.+?!=.+?$)


> The best I have done, so far, is this regex, with a space as its first character to ignore #ifdef and such:
> if(?s).+?\(.*?!.*?(;|\{)

Since (?s) enables . to match _across_ hard line breaks, you probably do not want to use it here since that's why the above pattern is matching the whole snippet.


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

F. Alfredo Rego

unread,
Jun 22, 2019, 5:11:25 PM6/22/19
to BBEdit Talk
Thanks, Patrick.

Alfredo
> --
> This is the BBEdit Talk public discussion group. If you have a
> feature request or need technical support, please email
> "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <https://www.twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To post to this group, send email to bbe...@googlegroups.com.
> Visit this group at https://groups.google.com/group/bbedit.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/F4F3F31D-1D95-4947-A536-DAB806BE2FB4%40barebones.com.

Christopher Stone

unread,
Jun 22, 2019, 6:41:29 PM6/22/19
to BBEdit-Talk
On 06/22/2019, at 14:15, Patrick Woolsey <pwoo...@barebones.com> wrote:
Based on your description above, I expect something like this should do the job, or at least serve as a starting point for further experimentation :-):

Search for:    (( |)if \(.+?!=.+?$)


Hey Patrick,

I expect I'd normally write that like this:

(\h?if \(.+?!=.+?$)

Is there any advantage to “( |)” other than being readable?


--
Take Care,
Chris

Patrick Woolsey

unread,
Jun 23, 2019, 11:52:44 AM6/23/19
to bbe...@googlegroups.com
On Jun 22, 2019, at 18:41, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 06/22/2019, at 14:15, Patrick Woolsey <pwoo...@barebones.com> wrote:
>> Based on your description above, I expect something like this should do the job, or at least serve as a starting point for further experimentation :-):
>>
>> Search for: (( |)if \(.+?!=.+?$)
>
> Hey Patrick,
>
> I expect I'd normally write that like this:
>
> (\h?if \(.+?!=.+?$)
>
> Is there any advantage to “( |)” other than being readable?

None of which I'm aware; that was mainly reflexive on my part. :-)
Reply all
Reply to author
Forward
0 new messages