使用正则表达式,我想检测字符串中的括号内是否存在特定单词,如果存在,请删除括号及其内容。
我想定位的词是:
picture
see
lorem
因此,这里有 3 个字符串示例:
$text1 = 'Hello world (see below).';
$text2 = 'Lorem ipsum (there is a picture here) world!';
$text3 = 'Attack on titan (is lorem) great but (should not be removed).';
我可以preg_replace()
使用什么正则表达式:
$text = preg_replace($regex, '' , $text);
删除这些括号及其内容(如果它们包含这些单词)?
结果应为:
$text1 = 'Hello world.';
$text2 = 'Lorem ipsum world!';
$text3 = 'Attack on titan great but (should not be removed).';
这是用于测试的 ideone。
您可以使用
以下方法(感谢@Casimir之前指出错误!
<?php
$regex = '~
('h*'( # capture groups, open brackets
[^)]*? # match everything BUT a closing bracket lazily
(?i:picture|see|lorem) # up to one of the words, case insensitive
[^)]*? # same construct as above
')) # up to a closing bracket
~x'; # verbose modifier
$text = array();
$text[] = 'Hello world (see below).';
$text[] = 'Lorem ipsum (there is a picture here) world!';
$text[] = 'Attack on titan (is lorem) great but (should not be removed).';
for ($i=0;$i<count($text);$i++)
$text[$i] = preg_replace($regex, '', $text[$i]);
print_r($text);
?>
观看有关 ideone.com 和 regex101.com 的演示。
您可以使用此正则表达式进行搜索:
'h*'([^)]*'b(?:picture|see|lorem)'b[^)]*')
这意味着
'h* # match 0 or more horizontal spaces
'( # match left (
[^)]* # match 0 or more of any char that is not )
'b # match a word boundary
(?:picture|see|lorem) # match any of the 3 keywords
'b # match a word boundary
[^)]* # match 0 or more of any char that is not )
') # match right )
并替换为空字符串:
正则表达式演示
法典:
$re = '/'h*'([^)]*'b(?:picture|see|lorem)'b[^)]*')/';
$result = preg_replace($re, '', $input);