I want to create regular expression that marks all phrases except those in A
tag.
I want to use it to replace it with link.
Can I dop it with one regular expression?
Here is my failed trial: https://regex101.com/r/3I2qvL/1
Asked
Active
Viewed 32 times
-2

piernik
- 3,507
- 3
- 42
- 84
-
Here's a hacky solution that works: `(?:
)\K|(TEST)`. Demo: https://regex101.com/r/3I2qvL/2. Make sure your discard the empty matches :) – degant May 31 '17 at 11:34 -
1@degant to get rid of the empty matches, you might use the skip&fail trick `/
(*SKIP)(*FAIL)|\btest\b/i`. See [demo](https://regex101.com/r/3I2qvL/3) – HamZa May 31 '17 at 11:41 -
Thanks @HamZa that's a neat trick. I couldn't figure out why `*SKIP` is needed before `*FAIL`? – degant May 31 '17 at 11:44
-
@degant check [this answer](https://stackoverflow.com/a/24535912) out – HamZa May 31 '17 at 11:45
-
@degant check [this page](https://meta.stackexchange.com/questions/230676/hey-you-yeah-you-post-your-answers-as-answers-not-comments/296481#296481) out – mickmackusa Jun 01 '17 at 00:06
1 Answers
1
To exclude matches surrounded by the tag match the tagged part first and then throw it away with \K
. This match should also be supplied with empty string via alternation to match substrings not starting with the tag:
(?:<a[^>]+>.*?<\/a>\K|)(^|\s|,|;|:|\.)(Test)($|\s|,|;|\.|\b)

Dmitry Egorov
- 9,542
- 3
- 22
- 40