我正在尝试构造一个与模式匹配的正则表达式:
word1, word2, word3
Run Code Online (Sandbox Code Playgroud)
所以基本上我希望" , "出现两次并在它们之间有单词.到目前为止,我提出了:
$general_content_check = preg_match("/^.*, .*$/", $general_content);
Run Code Online (Sandbox Code Playgroud)
但这, 在字符串中只有几次"匹配" .
请有人帮我这个吗?
这取决于你的意思"单词",但你可以从尝试这个开始:
^[^,]+(?:, +[^,]+){2}$
Run Code Online (Sandbox Code Playgroud)
说明:
^ Start of line/string.
[^,]+ A "word" (anything that isn't a comma - including whitespace, etc.)
(?: Start non-capturing group
, + A comma then any number of spaces
[^,]+ A word
) Close group
{2} Repeat group exactly two times
$ End of line/string.
"word"的其他可能定义:
[^\s,]+[A-Z]+可选添加不区分大小写的标志)\p{L}+不广泛支持)