Php 正则表达式:若字符串包含括号内的特定单词,则删除括号及其内容

Php 正则表达式:若字符串包含括号内的特定单词,则删除括号及其内容,php,regex,preg-replace,Php,Regex,Preg Replace,使用regex,我想检测字符串中是否有特定的单词存在于括号内,如果存在,请删除括号及其内容 我想说的话是: 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).

使用regex,我想检测字符串中是否有特定的单词存在于括号内,如果存在,请删除括号及其内容

我想说的话是:

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\u replace()
一起使用:

删除这些括号及其内容(如果包含这些词语)

结果应该是:

$text1 = 'Hello world.';
$text2 = 'Lorem ipsum world!';
$text3 = 'Attack on titan great but (should not be removed).';

这里有一个用于测试的正则表达式。

您可以使用此正则表达式进行搜索:

\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);

您可以使用以下方法(感谢@Casimir之前指出了一个错误!):



请参阅和。

如果这个问题符合条件,我将以50分奖励它。您到底被困在哪里?你有没有试过任何方法来达到你的目标?谢谢。是否有任何方法可以让正则表达式检测开始括号之前是否有空白,如果有,在移除括号时,也移除该空白。否则,将留下两个空格。另外,为了完成,请在preg_replace()语句中添加正则表达式。我在
之前有
\h*
它匹配0或可以更改为
\h+
的空格,以匹配
之前的一个或多个空格(
(请参阅更新的答案)感谢您的更新,尽管它不会检测括号是否在开始括号旁边没有空格(应该是空格)。请参见此示例:在您的输入中,
之后没有空格(
),并且它会删除
之前的空格(
。您对regex101突出显示的输出感到困惑。感谢您的代码Jan。尽管您的regex还有两个问题。如果开头的括号前面有空格,请在删除括号时删除该空格:它仍会检测到前面没有空格的括号。希望我讲得通。@HenrikPetterson:Updated,see I update the response。只需提及,您还可以使用
(?I:picture | see | lorem)
当您想要更改数组的项时,您可以使用引用操作符
使用
foreach
循环
foreach($text as&$item)$item=preg\u replace($regex),,$item);
。它比较短,但在这里没有用,因为
preg\u replace
可以将数组作为主题参数,所以:
$text=preg replace($regex,,$text)
应该足够了。在单词周围使用
*?
不能保证单词在括号内,因为
*?
可以匹配结束括号或开始括号。
$re = '/\h*\([^)]*\b(?:picture|see|lorem)\b[^)]*\)/'; 

$result = preg_replace($re, '', $input);
<?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);
?>