简体   繁体   English

在PHP中使用的正则表达式,用于从文本字符串中删除括号中的特定文本

[英]regular expression to use in PHP for removing particular text in brackets from a text string

Can't figure this one out... I need a regular expression to use in PHP for removing particular text from a text string... 无法弄清楚这一点...我需要一个正则表达式在PHP中使用以从文本字符串中删除特定文本...

My text strings are like this: "The product we're talking about [Art.0430000] is a good product" "The product we're talking about [Art.0430001] is a good product" "The product we're talking about [Art.7852000] is a good product" 我的文字字符串是这样的:“我们正在谈论的产品[Art.0430000]是一个好产品”“我们正在谈论的产品[Art.0430001]是一个好产品”关于[Art.7852000]是个好产品“

I need to remove [Art.0430000], [Art.0430001], [Art.7852000] from the strings... 我需要从字符串中删除[Art.0430000],[Art.0430001],[Art.7852000] ...

The regular expression should be: 正则表达式应为:

\[Art\.[0-9]+\]

(In words: The literal string "[Art.", followed by one or more numerals in the range 0-9, followed by the literal string "]".) (换句话说:文字字符串“ [Art。”,后跟0-9范围内的一个或多个数字,后跟文字字符串“]”。)

In PHP: 在PHP中:

$pattern = '/\[Art\.[0-9]+\]/';
$result = preg_replace($pattern, "", $string);

我没有测试过,但是应该可以工作:

preg_replace_all("/\[Art(^\]+)\]/", "", $string);

If you also want to compress the white space left over (2 spaces to 1 space); 如果您还想压缩剩余的空白(将2个空间压缩为1个空间);

$pattern = '/\[Art\.[0-9]+\]\s*/';
$result = preg_replace($pattern, "", $string);

$pattern = "#\\[Art\\.([o-9])+\\]#"; $ pattern =“#\\ [艺术\\。([o-9])+ \\]#”; str_replace($pattern,"",$str); str_replace($ pattern,“”,$ str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM