I’m stumped by a regex problem. I’ve been using them for a project and they have been working perfectly, but then I got to a situation I thought could be solved with a positive lookahead, but my attempt have not resulted as I have expected.
To illustrate I made a line below. Of it, I want to have two matches returned, the first of which contains “This” and “thisThing” and the other of which contains “That” and “thatThing”, where the first value can only be “This” or “That” and the second values can be any (\w*) pattern.
adsf(49) asd k:38* K.This(jfkael dk&$% thisThing) and K.That($$$$ djaf aj_dfj^^ ^ thatThing);
I thought that this would be something like:
K\.(This|That) (????????????????) (\w*)(?=\))
Meaning, Find “This” or “That” that occurs after “K.” and then get the first word preceding “)”. However, I don’t really understand how I can specify (look ahead until you find a “)”, and then backtrack until you get the word behind it. Is such a thing possible?
You could use a non-greedy quantifier:
or: (if you don’t want the close parenthesis to be part of the match)
This way it will match anything between This|That and the word, but it do so lazily (so as soon as it finds a good match for
\w*it will use it, and backtrack if there’s not a)following)