Ruby 为什么我的正则表达式在我要求它懒惰的时候却显得贪婪?

Ruby 为什么我的正则表达式在我要求它懒惰的时候却显得贪婪?,ruby,regex,Ruby,Regex,我正在使用Ruby 2.1 我的字符串是“这是我的用户配置文件(http://example.com/user/5316),很好。:” 我只想捕获括号中的URL 我的第一项工作是/(http:\/\/example.com.*.\)/。然后是任何字符,在遇到右括号时结束。但是不行,那不行。也许是贪婪,我想,懒惰会有所帮助。但是附加laziness操作符并没有帮助。我尝试使用[)]而不是\),但这也没用。我有点被难住了 我做错了什么?使用一个否定字符类,如[^\s]*匹配任何字符,但不匹配空格或)

我正在使用Ruby 2.1

我的字符串是
“这是我的用户配置文件(http://example.com/user/5316),很好。:”

我只想捕获括号中的URL

我的第一项工作是
/(http:\/\/example.com.*.\)/
。然后是任何字符,在遇到右括号时结束。但是不行,那不行。也许是贪婪,我想,懒惰会有所帮助。但是附加laziness操作符并没有帮助。我尝试使用
[)]
而不是
\)
,但这也没用。我有点被难住了


我做错了什么?

使用一个否定字符类,如
[^\s]*
匹配任何字符,但不匹配空格或
,零次或多次

> "Here's my user profile (http://example.com/user/420), it's good. :)".match(/\bhttp:\/\/example\.com\b[^\s)]*/)[0]
=> "http://example.com/user/420"

使用诸如
[^\s]*
之类的求反字符类,它匹配任何字符,但不匹配空格或
,零次或多次

> "Here's my user profile (http://example.com/user/420), it's good. :)".match(/\bhttp:\/\/example\.com\b[^\s)]*/)[0]
=> "http://example.com/user/420"

使用诸如
[^\s]*
之类的求反字符类,它匹配任何字符,但不匹配空格或
,零次或多次

> "Here's my user profile (http://example.com/user/420), it's good. :)".match(/\bhttp:\/\/example\.com\b[^\s)]*/)[0]
=> "http://example.com/user/420"

使用诸如
[^\s]*
之类的求反字符类,它匹配任何字符,但不匹配空格或
,零次或多次

> "Here's my user profile (http://example.com/user/420), it's good. :)".match(/\bhttp:\/\/example\.com\b[^\s)]*/)[0]
=> "http://example.com/user/420"

lazy操作符工作正常:

irb> m = "Here's my user profile (http://example.com/user/5316), it's good. :)".match(/(http:\/\/example.com.*?)\)/)
=> #<MatchData "http://example.com/user/5316)" 1:"http://example.com/user/5316">
irb> m[1]
=> "http://example.com/user/5316"
irb>m=“这是我的用户档案(http://example.com/user/5316),很好。:).match(/(http:\/\/example.com.*?)/)
=> #
irb>m[1]
=> "http://example.com/user/5316"

正则表达式应该是
/(http:\/\/example.com.*?)/
-惰性运算符包含

惰性运算符工作正常:

irb> m = "Here's my user profile (http://example.com/user/5316), it's good. :)".match(/(http:\/\/example.com.*?)\)/)
=> #<MatchData "http://example.com/user/5316)" 1:"http://example.com/user/5316">
irb> m[1]
=> "http://example.com/user/5316"
require "uri"

str = "Here's my user profile (http://example.com/user/5316), it's good. :)"
p URI.extract(str)
irb>m=“这是我的用户档案(http://example.com/user/5316),很好。:).match(/(http:\/\/example.com.*?)/)
=> #
irb>m[1]
=> "http://example.com/user/5316"

正则表达式应该是
/(http:\/\/example.com.*?)/
-惰性运算符包含

惰性运算符工作正常:

irb> m = "Here's my user profile (http://example.com/user/5316), it's good. :)".match(/(http:\/\/example.com.*?)\)/)
=> #<MatchData "http://example.com/user/5316)" 1:"http://example.com/user/5316">
irb> m[1]
=> "http://example.com/user/5316"
require "uri"

str = "Here's my user profile (http://example.com/user/5316), it's good. :)"
p URI.extract(str)
irb>m=“这是我的用户档案(http://example.com/user/5316),很好。:).match(/(http:\/\/example.com.*?)/)
=> #
irb>m[1]
=> "http://example.com/user/5316"

正则表达式应该是
/(http:\/\/example.com.*?)/
-惰性运算符包含

惰性运算符工作正常:

irb> m = "Here's my user profile (http://example.com/user/5316), it's good. :)".match(/(http:\/\/example.com.*?)\)/)
=> #<MatchData "http://example.com/user/5316)" 1:"http://example.com/user/5316">
irb> m[1]
=> "http://example.com/user/5316"
require "uri"

str = "Here's my user profile (http://example.com/user/5316), it's good. :)"
p URI.extract(str)
irb>m=“这是我的用户档案(http://example.com/user/5316),很好。:).match(/(http:\/\/example.com.*?)/)
=> #
irb>m[1]
=> "http://example.com/user/5316"

正则表达式应该是
/(http:\/\/example.com.*?)/
-惰性操作符包含

感谢您的回复。在你的例子中它确实有效,不幸的是,我在我的文章中没有提供足够的上下文。URL并不总是在括号中。它可能是
我的用户配置文件http://example.com/user/600
也是,所以我不能指望括号在那里。你应该更新问题以突出显示这一点。那么,您将如何确定URL的结尾呢?即使是另一个答案也需要捕获模式的终点。URL能包含空格吗?是的,我会更新帖子。我想URL会被任何非URL合法字符终止,比如空格。谢谢你的回复。在你的例子中它确实有效,不幸的是,我在我的文章中没有提供足够的上下文。URL并不总是在括号中。它可能是
我的用户配置文件http://example.com/user/600
也是,所以我不能指望括号在那里。你应该更新问题以突出显示这一点。那么,您将如何确定URL的结尾呢?即使是另一个答案也需要捕获模式的终点。URL能包含空格吗?是的,我会更新帖子。我想URL会被任何非URL合法字符终止,比如空格。谢谢你的回复。在你的例子中它确实有效,不幸的是,我在我的文章中没有提供足够的上下文。URL并不总是在括号中。它可能是
我的用户配置文件http://example.com/user/600
也是,所以我不能指望括号在那里。你应该更新问题以突出显示这一点。那么,您将如何确定URL的结尾呢?即使是另一个答案也需要捕获模式的终点。URL能包含空格吗?是的,我会更新帖子。我想URL会被任何非URL合法字符终止,比如空格。谢谢你的回复。在你的例子中它确实有效,不幸的是,我在我的文章中没有提供足够的上下文。URL并不总是在括号中。它可能是
我的用户配置文件http://example.com/user/600
也是,所以我不能指望括号在那里。你应该更新问题以突出显示这一点。那么,您将如何确定URL的结尾呢?即使是另一个答案也需要捕获模式的终点。URL能包含空格吗?是的,我会更新帖子。我想URL会被任何非URL合法字符终止,比如空格。你用的是什么“惰性操作符”<代码>/(http:\/\/example.com.*?\)/对我来说很好。您使用的是什么“惰性操作符”<代码>/(http:\/\/example.com.*?\)/对我来说很好。您使用的是什么“惰性操作符”<代码>/(http:\/\/example.com.*?\)/对我来说很好。您使用的是什么“惰性操作符”
/(http:\/\/example.com.*?\)/
对我来说很好。它选择括号和逗号。它选择括号和逗号。它选择括号和逗号。它选择括号和逗号。它选择括号和逗号。
require "uri"

str = "Here's my user profile (http://example.com/user/5316), it's good. :)"
p URI.extract(str)