简体   繁体   English

Ruby正则表达式的版本号

[英]Ruby regular expression for version numbers

I want to write a program which takes build number in the format of 23.0.23.345 (first two-digits then dot, then zero, then dot, then two-digits, dot, three-digits): 我想编写一个程序,该程序的内部版本号为23.0.23.345 (前两位,然后是点,然后是零,然后是点,然后是两位,点,三位):

number=23.0.23.345
pattern = /(^[0-9]+\.{0}\.[0-9]+\.[0-9]$)/

numbers.each do |number| 
    if number.match pattern
        puts "#{number} matches"
    else
        puts "#{number} does not match"
    end
end

Output: I am getting error: floating literal anymore put zero before dot 输出:我遇到错误:浮点文字不再在点前放置零

I'd use something like this to find patterns that match: 我会用类似这样的东西来找到匹配的模式:

number = 'foo 1.2.3.4 23.0.23.345 bar'
build_number = number[/
  \d{2} # two digits
  \. 
  0 
  \. 
  \d{2} # two more digits
  \.
  \d{3}
/x]
build_number # => "23.0.23.345"

This example is using String's [/regex/] method, which is a nice shorthand way to apply and return the result of a regex. 此示例使用String的[/regex/]方法,这是套用和返回regex结果的简便方法。 It returns the first match only in the form I'm using. 它仅以我使用的形式返回第一个匹配项。 Read the documentation for more information and examples. 阅读文档以获取更多信息和示例。

Your pattern won't work because it doesn't do what you think it does. 您的模式不起作用,因为它没有按照您的想象做。 Here's how I'd read it: 这是我的阅读方式:

/(       # group
  ^      # start of line
  [0-9]+ # one or more digits
  \.{0}  # *NO* dots
  \.     # one dot
  [0-9]+ # one or more digits
  \.     # one dot
  [0-9]  # one digit
  $      # end of line
)/x

The problem is \\.{0} which means you don't want any dots. 问题是\\.{0} ,这意味着您不需要任何点。

The x flag tells Ruby to use multiline, which ignores blanks/whitespace and comments, making it easy to build a pattern that is documented. x标志告诉Ruby使用多行,它会忽略空格/空格和注释,从而很容易构建记录的模式。

Why reinvent the wheel? 为什么要重新发明轮子? Use a gem like versionomy . 使用像versionomy这样的宝石。 You can parse the versions, compare them, check for equality, increment a particular part, etc. It even handles alpha, beta, patchlevels, etc. 您可以解析版本,比较它们,检查是否相等,增加特定部分等。它甚至可以处理alpha,beta,补丁级别等。

require 'versionomy'

number='23.0.23.345'

v = Versionomy.parse number
v.major #=> 23
v.minor #=> 0
v.tiny  #=> 23
v.tiny2 #=> 345
numbers = "23.0.23.345", "23.0.33.173", "0.0.0.0"
pattern = /\d{2}\.0\.\d{2}\.\d{3}/x

numbers.each do |number| 
   if number.match pattern
    puts "#{number} matches"
   else
    puts "#{number} does not match"
   end
end

The "number" array in line one needs to have values of strings and not integers, I also changed the array "number" to "numbers", you will also need multiple items in the numbers array to call the ".each" method in your loop. 第一行中的“数字”数组需要具有字符串值,而不是整数,我也将“数字”数组更改为“数字”,在数字数组中还需要多个项才能在其中调用“ .each”方法你的循环。

There seems to be agreement on what regular expression you should use. 您应该使用哪种正则表达式似乎已达成共识。 If your ultimate goal is to extract the elements of the strings as integers, you could do this: 如果您的最终目标是将字符串的元素提取为整数,则可以执行以下操作:

str = "I'm looking for 23.0.345.26, or was that 23.0.26.345?"
str.scan(/(\d{2})\.(0)\.(\d{2})\.(\d{3})/).flatten.map(&:to_i)
  #=> [23, 0, 26, 345]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM