[英]Match substring of a substring
Input: 输入:
Animal {cow.<span>moo</span>} <span>noises</span>
Output: 输出:
Animal {cow.moo} <span>noises</span>
How could I match only the <span>
inside the braces for replacement? 我怎么能只匹配支架内的
<span>
进行更换? I have got as far as matching everything between the braces with 我已经在括号之间匹配了所有内容
(?<=\{)(.*?)(?=\})
Any help would be greatly appreciated. 任何帮助将不胜感激。
You may use a preg_replace_callback
to match the strings in between {...}
with a basic regex like '~{[^}]+}~'
and then replace what you need inside the callback function: 你可以使用
preg_replace_callback
来匹配{...}
之间的字符串和'~{[^}]+}~'
类的基本正则表达式,然后在回调函数中替换你需要的内容:
$s = 'Animal {cow.<span>moo</span>} <span>noises</span>';
echo preg_replace_callback('~{[^}]+}~', function($m) {
return str_replace(["<span>", "</span>"], "", $m[0]);
}, $s);
// => Animal {cow.moo} <span>noises</span>
You may use a preg_replace
inside the callback function if you need to replace with a regex. 如果需要用正则表达式替换,可以在回调函数中使用
preg_replace
。
If you want to capture the moo
inside the span
for substitution you can use this regexp: 如果要捕获
span
内的moo
以进行替换,可以使用此正则表达式:
\{.*?\<span\>(.*)\<\/span\>\}
Here is an example: https://regex101.com/r/ryP3Y6/1 以下是一个示例: https : //regex101.com/r/ryP3Y6/1
If you want to delete the <span>
tags you can use: 如果要删除
<span>
标记,可以使用:
(.*\{.*?)(?:\<[\/]?span\>)(.*)(?:<\/span\>)(\}.*)
And use \\1\\2\\3
for substitution: 并使用
\\1\\2\\3
替换:
Check: https://regex101.com/r/qECuai/1 检查: https : //regex101.com/r/qECuai/1
With a \\G
based pattern: 使用基于
\\G
的模式:
$str = preg_replace('~(?:\G(?!\A)|{)[^}]*?\K</?span>~', '', $str);
This pattern starts with two possible branches: 此模式从两个可能的分支开始:
(?=[^}]*})
after it to ensure there's a closing bracket. (?=[^}]*})
以确保有一个结束括号。 \\G
that is the position after the last match. \\G
开头,即最后一个匹配后的位置。 [^}]*?
forbids to go out of the curly bracket enclosed substring. 禁止走出大括号括起来的子串。
This design ensures that all series of matches starting from a { are contiguous and that the <span>
tags found are between curly brackets. 此设计确保从{开始的所有系列匹配都是连续的,并且找到的
<span>
标记位于大括号之间。
For small strings and if you are sure curly brackets are balanced and not nested, you can also use a more simple pattern: 对于小字符串,如果您确定大括号是平衡的而不是嵌套的,您还可以使用更简单的模式:
$str = preg_replace('~</?span>(?=[^{}]*})~', '', $str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.