Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match_如果通配符包含斜杠,则停止_Php_Regex - Fatal编程技术网

Php preg_match_如果通配符包含斜杠,则停止

Php preg_match_如果通配符包含斜杠,则停止,php,regex,Php,Regex,假设我有一个正则表达式:“#artists/(.*)/#”,我想匹配这个字符串:”/artists/alesana/wires和呼吸的概念,我如何确保它只匹配“alesana”,而不匹配“alesana/wires和呼吸的概念” 换句话说,我怎样才能让我的reg exp与斜线保持一致。 从技术上讲,我将为艺术家/(.*)/(.*)创建另一个路由规则,但我知道我迟早会在其他地方遇到这个问题。使用?字符使正则表达式不贪婪。这将基本上找到可能的最短匹配: /artists/(.*?)/ 阅读更多内容

假设我有一个正则表达式:
“#artists/(.*)/#”
,我想匹配这个字符串:
”/artists/alesana/wires和呼吸的概念
,我如何确保它只匹配
“alesana”
,而不匹配
“alesana/wires和呼吸的概念”

换句话说,我怎样才能让我的reg exp与斜线保持一致。
从技术上讲,我将为
艺术家/(.*)/(.*)
创建另一个路由规则,但我知道我迟早会在其他地方遇到这个问题。

使用
字符使正则表达式不贪婪。这将基本上找到可能的最短匹配:

/artists/(.*?)/

阅读更多内容:

您尝试过这个吗
artists/[^/].+?/
除了使用Hans Engel提到的非贪婪搜索之外,另一种解决方案是:

/artists/([^/]*)/
在字符类内使用
^
将否定内容。因此,
[^/]
将匹配除斜杠以外的所有字符


有关正则表达式和元字符的更多信息,请访问

Ahh。。我知道这会很简单。希望我不会再犯这个错误了。谢谢你真的想避免贪婪的操作符,而选择非贪婪的操作符。贪婪的操作员在评估时往往会消耗更多的性能。尽管在许多情况下这是正确的,但在这里情况并非如此。在这种情况下,使用惰性量词实际上会导致比贪婪量词多得多的操作,因为需要所有的回溯。