SlickEdit Community

SlickEdit Product Discussion => SlickEdit® => Topic started by: jim on November 14, 2008, 09:57:27 PM

Title: Regex (Negative Lookahead) Question
Post by: jim on November 14, 2008, 09:57:27 PM
Hi folks,

Just a sanity check, I think I might be misunderstanding how negative lookahead is supposed to work.  Suppose the following (contrived) target text:

Code: [Select]
user_id
admin_user_id

My understanding is that this regex:

Code: [Select]
(?!admin_)user_id
... should match user_id, but not match admin_user_id (I thought the negative lookahead assertion, if matched, should cause the regex to fail).  However, what SE seems to do (in Perl regex mode, by the way), is match user_id, and just the user_id portion of admin_user_id.  What I want is to not match admin_user_id (in whole or in part) at all.

Thanks!

Jim
Title: Re: Regex (Negative Lookahead) Question
Post by: Lee on November 14, 2008, 11:02:18 PM
The expression is working correctly, but you are not going to be able to do that with negative lookahead expression.  The expression looks ahead of the cursor, if it does not match admin, then continue to next part of the expression.  Perl does have negative lookbehind (?<!) which does look behind the current cursor position, unfortunately SlickEdit regex implementation does not yet support lookbehind expressions. SlickEdit does support word boundaries (\b in Perl regex), so in the contrived sample it would match user_id but not admin_user_id.
Title: Re: Regex (Negative Lookahead) Question
Post by: jim on November 15, 2008, 12:14:41 AM
Thanks, Lee.  So is this explanation in the SE help incorrect?

Quote
(?!X) ... Search fails if expression X is matched. The expression ^(?!if) matches the beginning of all lines that do not start with if.

I thought, from that example, that my usage would work.

If the help is correct, I'm afraid I still don't get it.

Thanks again!