Search code examples
regextasker

Regex match if first instance of string is followed by string -- force lazy


I am trying to return a match only if the first instance of a string is followed by another string. However they may appear later on, but I don't want a match then.

Thing #1
.........
hidden=true
.........
Thing #2
.........
hidden=false
.........

Right now I am using Thing #1[\S\s]*?hidden=false but obviously that will return a match for the above example when I don't want it to. Only if the first hidden= is followed by false. I know how it could be done with a capture group but that would greatly complicate things for me, I am using Tasker on Android and capture groups are a huge ordeal and slow. How can I do this?


Solution

  • You're close. You just need to prevent another Thing #1 from being between the matched thing and the next marker.

    Use a negative look ahead:

    Thing #1((?!Thing #1)[\S\s])*?hidden=false