Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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/17.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
Ruby 使用正则表达式检测版本号的特定格式_Ruby_Regex - Fatal编程技术网

Ruby 使用正则表达式检测版本号的特定格式

Ruby 使用正则表达式检测版本号的特定格式,ruby,regex,Ruby,Regex,我希望提取包含版本号的数组元素,其中版本号位于字符串的开头或结尾或由空格填充,是一系列数字和句点,但不以句点开头或结尾。例如,“10.10 Thingy”和“Thingy 10.10.5”是有效的,但“which 4”不是 haystack=[“10.10 Thingy”、“Thingy 10.10.5”、“任意4”、“任意4.x”] haystack.选择{i | i[/(?[“10.10 Thingy”,“Thingy 10.10.5”,“任意4”] 我不知道如何修改正则表达式,使其至少需

我希望提取包含版本号的数组元素,其中版本号位于字符串的开头或结尾或由空格填充,是一系列数字和句点,但不以句点开头或结尾。例如,“10.10 Thingy”和“Thingy 10.10.5”是有效的,但“which 4”不是

haystack=[“10.10 Thingy”、“Thingy 10.10.5”、“任意4”、“任意4.x”]
haystack.选择{i | i[/(?[“10.10 Thingy”,“Thingy 10.10.5”,“任意4”]

我不知道如何修改正则表达式,使其至少需要一个句点,这样结果中就不会出现“Whatever 4”。

啊哈!我知道我已经接近了


haystack.选择{| i | i[/(?这只是Archonic答案的一个细微变化

r = /
    (?<=\A|\s) # match the beginning of the string or a space in a positive lookbehind
    (?:\d+\.)+ # match >= 1 digits followed by a period in a non-capture group, >= 1 times 
    \d+        # match >= 1 digits
    (?=\s|\z)  # match a space or the end of the string in a positive lookahead
    /x         # free-spacing regex definition mode

haystack = ["10.10 Thingy", "Thingy 10.10.5", "Whatever 4", "Whatever 4.x"]

haystack.select { |str| str =~ r }
  #=> ["10.10 Thingy", "Thingy 10.10.5"]
假设您希望获取包含有效版本的字符串以及这些字符串中包含的版本。您可以编写以下代码:

r = /
    (?<=\A|\s\) # match the beginning of string or a space in a pos lookbehind
    (?:\d+\.)+  # match >= 1 digits then a period in non-capture group, >= 1 times 
    \d+         # match >= 1 digits
    (?=\s|\z)   # match a space or end of string in a pos lookahead
    /x          # free-spacing regex definition mode

haystack.each_with_object({}) do |str,h|
  version = str[r]
  h[str] = version if version
end
  # => {"10.10 Thingy"=>"10.10", "Thingy 10.10.5"=>"10.10.5"}
r=/
(?=1位,然后是非捕获组中的一个周期,>=1次
\d+#匹配>=1位
(?=\s |\z)#匹配pos前瞻中的空格或字符串结尾
/x#自由间距正则表达式定义模式
haystack.each_with_object({})do|str,h|
版本=str[r]
h[str]=版本如果版本
结束
#=>{“10.10 Thingy”=>“10.10”,“Thingy 10.10.5”=>“10.10.5”}

谢谢您提供的详细信息!
r = /
    [\A\s\]    # match the beginning of the string or a space
    (?:\d+\.)+ # match >= 1 digits followed by a period in a non-capture group, >= 1 times 
    \d+        # match >= 1 digits
    [\s\z]     # match a space or the end of the string in a positive lookahead
    /x         # free-spacing regex definition mode

haystack.select { |str| str =~ r }
  #=> ["10.10 Thingy", "Thingy 10.10.5"]
r = /
    (?<=\A|\s\) # match the beginning of string or a space in a pos lookbehind
    (?:\d+\.)+  # match >= 1 digits then a period in non-capture group, >= 1 times 
    \d+         # match >= 1 digits
    (?=\s|\z)   # match a space or end of string in a pos lookahead
    /x          # free-spacing regex definition mode

haystack.each_with_object({}) do |str,h|
  version = str[r]
  h[str] = version if version
end
  # => {"10.10 Thingy"=>"10.10", "Thingy 10.10.5"=>"10.10.5"}