我正在try 编写一个Regex验证器(Python3.8)来接受如下字符串:
foo
foo,bar
foo, bar
foo , bar
foo , bar
foo, bar,foobar
这就是我到目前为止所掌握的(但它只与前两个 case 相匹配):
^[a-zA-Z][0-9a-zA-Z]+(,[a-zA-Z][0-9a-zA-Z]+)*$|^[a-zA-Z][0-9a-zA-Z]+
但是,当我添加空格匹配\w
时,它完全停止匹配:
^[a-zA-Z][0-9a-zA-Z]+(\w+,\w+[a-zA-Z][0-9a-zA-Z]+)*$|^[a-zA-Z][0-9a-zA-Z]+
要使用的模式是什么(并解释了为什么上面的第二个模式不匹配).