如何在python re中编写regex表达式,其中模式为:3个英文字母后接逗号
示例:
string_var = "AAA, BBB, CCC" # follows the pattern
string_var = "AAA, BBBB, CCC, DDD" # does not follow the pattern
如果只想检查字符串是否匹配,请使用^[A-Za-z]{3}, [A-Za-z]{3}, [A-Za-z]{3}$
(https://regexr.com/5i22s)。对于";AAA、BBB、CCC";,但是对于像在"匹配"中那样查找匹配是不有用的;测试asf,猫,狗";。
^ Start of string
[A-Za-z] Alphabet characters
{3} 3 alphabet characters
, A comma followed by a space
...
$ End of string
试试这个
^([a-zA-Z]{3},s){2}[a-zA-Z]{3}$
或将其与re.I
标志一起使用
^([a-z]{3},s){2}[a-z]{3}$