I’m to find a regular expression to match strings inside single but not multiple particular characters. For example:
this is =one= and =two= but not ==three== or ==four== etc
Here I’m using =
as a delimiter, and I want to match the one
and two
because they’re inside single delimiters but not three
or four
because they’re inside multiple delimiters.
I thought using negative lookahead and lookbehind would do the trick:
/(?<!=)=(.+?)=(?!=)/g
but I end up matching the other strings as well, so I obviously don’t understand properly hot negative lookahead and lookbehind work. I thought the expression means =
not preceded by a =
, and so on.
A bigger test includes multiline strings:
this is =one= and =two= but not ==three== or ==four==.
====
this shouldn’t count at all
====
There might be another approach, which would be welcome, but one using lookahead and lookbehind would be more educational I think.
I’m using PHP and I plan to use the preg_replace_callback()
function.
=(.+?)=
- the =
match the "outer" of the double ==
here, but then .+
still allows =
in that position as well.
(?<!=)=([^=]+)=(?!=)
should do the trick.