Given a text string (a markdown document) I need to achieve one of this two options:
to replace all the matches of a particular expression (
(\W)(theWord)(\W)
) all across the document EXCEPT the matches that are inside a markdown image syntax![Blah theWord blah](url)
.to replace all the matches of a particular expression (
{{([^}}]+)}}\[\[[^\]\]]+\]\]
) ONLY inside the markdown images, ie.:![Blah {{theWord}}[[1234]] blah](url)
.
Both expressions are currently matching everything, no matter if inside the markdown image syntax or not, and I've already tried everything I could think.
Here is an example of the first option
And here is an example of the second option
Any help and/or clue will be highly appreciated.
Thanks in advance!
The first regex is easily fixed with a SKIP-FAIL trick:
To replace with the word of your choice. It is a totally valid way in PHP (PCRE) regex to match something outside some markers.
See Demo 1
As for the second one, it is harder, but acheivable with
\G
that ensures we match consecutively inside some markers:To replace with
$1$2{{NEW_REPLACED_TEXT}}[[NEW_DIGITS]]
See Demo 2
PHP:
You can leverage the discard technique that it really useful for this cases. It consists of having below pattern:
So, according you needs:
You can easily achieve this in
pcre
through(*SKIP)(*FAIL)
flags, so for you case you can use a regex like this:Or using your pattern:
The idea behind this regex is tell regex engine to skip the content within
[
...]
Working demo
Well I modified first expression a little bit as I thought there are some extra capturing groups then made them by adding a lookahead trick:
-First one (Live demo):
-Second one (Live demo):
Lookahead part explanations:
(?=
means a positive lookahead