admin管理员组文章数量:1355607
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.
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.
1 Answer
Reset to default 3=(.+?)=
- the =
match the "outer" of the double ==
here, but then .+
still allows =
in that position as well.
(?<!=)=([^=]+)=(?!=)
should do the trick.
版权声明:本文标题:php - Matching text inside single but not multple delimiter characters in a regular expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744050500a2582334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
=(.+?)=
- the=
match the "outer" of the double==
here, but then.+
still allows=
in that position as well.(?<!=)=([^=]+)=(?!=)
should do the trick. – C3roe Commented Mar 28 at 7:55