Author Topic: Help with regex search, negative lookahead  (Read 389 times)

rowbearto

  • Senior Community Member
  • Posts: 1484
  • Hero Points: 113
Help with regex search, negative lookahead
« on: October 16, 2019, 07:26:42 pm »
I want to do a search for testcase.XXXXX, where XXXX can be anything but exclude cases where XXXX=testObjInstance

For example if my file contains:

testcase.abcd
testcase.testObjInstance

I want to find "testcase.abcd" but not have "testcase.testObjInstance" show up in my search results.

I tried using BigFind regex "negative lookahead" as advised in stack overflow:  https://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string

For "Search For:" I used:

(?!testObjInstance)testcase\.

But it finds both "testcase.abcd" and "testcase.testObjInstance".

I then changed to "Vim syntax" but it finds nothing.

What is the best way to do this in Slick?

rowbearto

  • Senior Community Member
  • Posts: 1484
  • Hero Points: 113
Re: Help with regex search, negative lookahead
« Reply #1 on: October 16, 2019, 07:49:44 pm »
OK, I found an expression that works. Had to wrap with .* :

(?!.*testObjInstance.*)testcase\.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 4937
  • Hero Points: 404
Re: Help with regex search, negative lookahead
« Reply #2 on: October 16, 2019, 08:00:17 pm »
I think you want something like this:

testcase\.(?!testObjInstance)

What you wrote works but is less efficient

TKasparek

  • Senior Community Member
  • Posts: 216
  • Hero Points: 27
Re: Help with regex search, negative lookahead
« Reply #3 on: October 17, 2019, 10:33:19 pm »
My two regex cheat bookmarks (I use PCRE)  ;):
https://regex101.com/
https://www.debuggex.com/cheatsheet/regex/pcre

You should use Clark's suggestion. If you use yours and your negative instance is in the search area you'll negate a positive search due to the wildcards.
For instance
testcase.abcd testcase.testObjInstance
on the same line will match
testcase.abcd
with Clark's but not yours.

Also, if you want to include the extension...
testcase\.(?!testObjInstance)\S+
https://regex101.com/r/Q5EXTc/1

rowbearto

  • Senior Community Member
  • Posts: 1484
  • Hero Points: 113
Re: Help with regex search, negative lookahead
« Reply #4 on: October 21, 2019, 04:28:46 pm »
Thanks Clark and TKasparek! Very very helpful.