5 ms·
> Match Tarzan but not "Tarzan" Unfortunately it doesn't work. Let's say I wanted to match a string following Tarzan but not "Tarzan", I will try his techniqu
by smegel 11y ago
> Match Tarzan but not "Tarzan"
Unfortunately it doesn't work.
Let's say I wanted to match a string following Tarzan but not "Tarzan", I will try his technique:
("Tarzan"|(Tarzan))\s+and JillOfTheJungle
Unfortunately this matches both:
"Tarzan" and JillOfTheJungle
and
Tarzan and JillOfTheJungle
Or maybe he meant:
> Capture Tarzan but not "Tarzan"
- geocar 11y agoa=/(?:"Tarzan"|(Tarzan))\s+and JillOfTheJungle/; matched = !!a.exec(x)[1] Works fine. People often forget to solve the problem they need to solve (match x), and instead work on other things (find a regex to match x).
- jameshart 11y ago"People often forget to solve the problem they need to solve (match x), and instead work on other things (find a regex to match x)." Completely agree. One common mistake is building a complex regex to match the elements you want to find from a string, when an easier approach is to split the string on a simple regex that matches the things you want to throw away.
- jameshart 11y agoYes, when you apply a technique incorrectly, it often doesn't work. "Tarzan"\s+and JillOfTheJungle|(Tarzan\s+and JillOfTheJungle) Seems to meet your needs?
- smegel 11y agoNo I dont want to capture anything, I just want to match a string that contains Tarzan but not "Tarzan". Your example seems to match both both. Is that too much to ask?
- jameshart 11y agoNo, it's not. Just run through the matches returned by this regex, and reject the ones which don't have a captured group. Done. Did you want a more complex solution?