有人能帮我处理这个正则表达式吗?因为我不知道如何实现它。
我需要一个正则表达式来删除字符串中的所有单词,该字符串至少包含一个字符,该字符不是UTF-8字母或数字,也不是单词中间的标点符号(但不是结尾)。
示例:
This is ®Aix string
A bad str?ng is here
第一个示例包含®
,它不是字母、数字或标点符号。第二个例子在在中间包含标点符号。
我需要删除这些不好的单词,但保持字符串的其余部分不变。例如This is string
、A bad is here
。
请注意,A bad string? is here
不会包含任何坏单词,因为标点符号位于单词末尾。
提前感谢您的帮助。
这个怎么样:
$result = preg_replace(
'/'b # Start of word
['p{L}'p{N}]+ # One or more Unicode letters
[^'s'p{L}'p{N}] # One non-letter (and non-whitespace), followed by
[^'s'p{P}]+ # at least one non-whitespace, non-punctuation character
'b # End of word
's* # optional following whitespace
/xu',
'', $subject);