[英]How to remove text from simple parentheses using regex in PHP?
Problem: Text sample - "Wanted String (unwanted)"
. 问题:文本示例-
"Wanted String (unwanted)"
。 There is only one string inside parentheses. 括号内只有一个字符串。
I need to remove the "(unwanted)"
including parentheses using RegEx in PHP. 我需要在PHP中使用RegEx删除包括括号在内的
"(unwanted)"
。
If you have only one pair of parentheses, you can use the following two regexes and then concatenate their result. 如果只有一对括号,则可以使用以下两个正则表达式,然后将其结果串联起来。 For eg, if your string is
Wanted String (unwanted) and something after it
, then: 例如,如果您的字符串是
Wanted String (unwanted) and something after it
,则:
[^\\(]*
will return Wanted String
. (Working example: https://regex101.com/r/vZ6eG4/1 ) [^\\(]*
将返回Wanted String
(工作示例: https : //regex101.com/r/vZ6eG4/1 ) (?<=\\)).*
will return and something after it
. (?<=\\)).*
将返回and something after it
返回。 (Working example: https://regex101.com/r/wL3aO9/1 ) $result = preg_replace('/\(.*?\)/', '', $subject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.