I'm trying to construct a regular expression that would match a pattern as such:
word1, word2, word3
So basically I want ",
" to appear twice and to have words between them. So far I came up with:
$general_content_check = preg_match("/^.*, .*$/", $general_content);
But this matches only ",
" several times in a string.
Can someone help me with this please?
It depends what you mean by "word" but you can start by trying this:
^[^,]+(?:, +[^,]+){2}$
Explanation:
^ 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.
Other possible definitions of "word":
[^\s,]+
[A-Z]+
(optionally add case-insensitive flag)\p{L}+
(not widely supported)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With