[英]the meaning of a ruby regular expression
string=http://abc.com.vn/12345
string =~ /.*\/([0-9]+)/
number = $~[1]
在上面使用Ruby正则表达式的代码行中,变量“ number”的值将为“ 12345”。 但是,我不清楚上面使用的〜/.*/([0-9]+)/和$〜[1]的含义。 你能帮我解释一下吗? 非常感谢。
根据Ruby Doc的说法, $~
全局变量等效于last_match
方法。 来源 。
前一行=~ /.*\\/([0-9]+)/
检查在字符串中存储的string
和右侧的正则表达式之间是否匹配。 =~
将返回该字符串中有匹配项的数字位置,如果没有匹配项,则返回nil
。
你应该阅读有关红宝石regexs最小这里发布提问之前,但这个是让你开始:
=~ is the match operator similar to the match operator in perl
$~ is an array holding the various elements of the match
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.