Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Objective c 从末尾搜索时的非贪婪正则表达式匹配_Objective C_Regex_Nsregularexpression - Fatal编程技术网

Objective c 从末尾搜索时的非贪婪正则表达式匹配

Objective c 从末尾搜索时的非贪婪正则表达式匹配,objective-c,regex,nsregularexpression,Objective C,Regex,Nsregularexpression,我读到您应该使用?非贪婪地匹配文本,因此正则表达式 http://.\.png …用于 http://example.png.png …将返回http://example.png 但不贪婪似乎只是从左到右起作用。也就是说,如果我匹配它的话 http://http://example.png …它将返回http://http://example.png 如何获得匹配的代码http://example.png仅限?尝试以下方法: http://[A-Za-z0-9_-]+\.png 它不会得

我读到您应该使用
非贪婪地匹配文本,因此正则表达式

http://.\.png
…用于

http://example.png.png
…将返回
http://example.png

但不贪婪似乎只是从左到右起作用。也就是说,如果我匹配它的话

http://http://example.png
…它将返回
http://http://example.png

如何获得匹配的代码
http://example.png
仅限?

尝试以下方法:

http://[A-Za-z0-9_-]+\.png
它不会得到第一个
http://
,因为它与
.png

如果您担心URL中的其他字符,也可以使用此选项:

http://[^:]+?\.png

你也可以用消极的眼光看待未来,但我认为斯默尼的答案更好

http://(?!http://).*?\.png

谢谢所有的URL都只包含A-Z、A-Z、0-9、-、\ux和%?老实说,我从来没有见过包含
%
的URL。您可以将
[A-Za-z0-9_-]
部分替换为
[^::+?
,如果您对此感到担心,它可能适用于您的任何情况。哦,我刚刚意识到带有端口号的URL有问题。例如,这是一个有效的url,但我不认为它与这个正则表达式匹配。没错,如果这是一个问题,我想负面展望会更好。我有点好奇这个用例是什么。