Author Topic: Regex (Negative Lookahead) Question  (Read 5518 times)

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Regex (Negative Lookahead) Question
« 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

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Regex (Negative Lookahead) Question
« Reply #1 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.

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Regex (Negative Lookahead) Question
« Reply #2 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!