[英]Find words containing special characters with PHP regex
我的句子中包含一些错误的单词(错误的单词包含超过1个特殊字符: == >> << ?? .. ,, !! ## $$ %% // ** -- ++ ex
...
我使用preg_match_all()
进行计数,但结果错误。 你能帮我解决吗
str = "you he==rt are my heart 4++5=9 9-8===1"
$pattern = '/[^\s]*(?:[\+]{2,}|[\%]{2,}|[\-]{2,}|[=]{2,}|[<]{2,}|[>]{2,}|[\*]{2,}|[\/]{2,}|[\:]{2,}|[\.]{2,}|[\?]{2,})/';
结果
he==
4++
8===
我需要找到结果
he==rt
4++5=9
9-8===1
谢谢!
您可以使用
(?=(?:\S*[^\w\s]){2,})\S+
细节:
(?=(?:\\S*[^\\w\\s]){2,})
-一个正向超前检查,检查当前位置之后是否存在以下2个或更多序列:
\\S*
-0+个非空白字符,后跟 [^\\w\\s]
-单词或空格以外的字符 \\S+
-匹配1+个非空白字符 IDEONE演示 :
$re = '~(?=(?:\S*[^\w\s]){2,})\S+~';
$str = "you he==rt are my heart 4++5=9 9-8===1 hyphenated-word";
preg_match_all($re, $str, $matches);
print_r($matches[0]); // Array( [0] => he==rt [1] => 4++5=9 [2] => 9-8===1 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.