这个正则表达式有什么问题,试图匹配模式名称1.name2


What is wrong with this regular expression, trying to match pattern name1.name2?

我正在尝试将模式"john.doe"与此正则表达式匹配:

 ^(([a-zA-Z0-9_'.-]+)'.([a-zA-Z0-9_'.-]))/?$

我无法让它匹配。

正则表达式只是过大

list($name1, $name2) = explode('.', $name, 2);

则表达式/^(['w'-]+)'.(['w'-]+)$/应该适用于您要执行的操作。

就像德米特里·丘巴罗夫(Dmitri Chubarov)说的(归功于他),你只需要在第二组添加一个+符号:

^(([a-zA-Z0-9_'.-]+)'.([a-zA-Z0-9_'.-]+))/?$

^((([a-zA-Z0-9_-]+'.)*[a-zA-Z0-9_-]+)'.(([a-zA-Z0-9_-]+'.)*[a-zA-Z0-9_-]+))/?$

这将适用于:

jhon.doe
this.also.work
this.will.also.work

但不是:

x..
..x
..
...
....
.....

http://regexpal.com/?flags=gm&regex=%5E(((%3F%3A%5Ba-zA-Z0-9_-%5D%2B%5C.)*%5Ba-zA-Z0-9_-%5D%2B)%5C.((%3F%3A%5Ba-zA-Z0-9_-%5D%2B%5C.)*%5Ba-zA-Z0-9_-%5D%2B))%2F%3F%24&input=john.doe%0Afirst.second%0Aand.this.works%0Aand.this.also.works%0Ax.x%0Ax..%0A..x%0A..%0A...%0A....%0A.....