正则表达式匹配数字

时间:2013-09-05 12:56:37

标签: ruby regex

我正在寻找一个正则表达式regex来匹配其中一种模式:

  • 数字后跟x
  • 以一个或多个空格分隔的数字

我不知道match是否是实现结果的正确方法。

匹配示例:

' 30x '
'30x'
'20 30'
' 20 30 '

'30x'.match(regex).to_a #=> ['30']
'30 40'.match(regex).to_a #=> ['30', '40']
"30".match(regex).to_a # => ["30"]
" 30 ".match(regex).to_a # => ["30"]
"30 40".match(regex).to_a # => ["30", "40"]

不匹配的例子:

'20x 30 '
'x20 '

"30xx".match(regex).to_a # => nil
"30 a".match(regex).to_a # => nil
"30 60x".match(regex).to_a # => nil
"30x 20".match(regex).to_a # => nil

修改

根据@TeroTilus建议,这是此问题的用例:

用户将插入他将如何偿还债务。然后,我们创建了一个文本域 轻松插入付款条件。例如:

 > "15 20" # Generate 2 bills: First for 15 days and second for 20 days
 > "2x" # Generate 2 bills: First for 30 days and second for 60 days 
 > "2x 30" # Show message of 'Invalid Format'
 > "ANY other string" # Show message of 'Invalid Format'

5 个答案:

答案 0 :(得分:1)

怎么样:

/^\s*\d+(?:x\s*|\s*\d+)?$/

<强>解释

The regular expression:

(?-imsx:^\s*\d+(?:x\s*|\s*\d+)?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    x                        'x'
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

答案 1 :(得分:0)

尝试string.scan(/(^|\s+)(\d+)x?/).map(&:last),它可能会做你想要的。

答案 2 :(得分:0)

这适用于您提供的示例。

^(?:\s*(\d+))+x?\s*$

^     # Match start of string
(?:   # Open non-capturing group
\s*   # Zero or more spaces at start or between numbers
(\d+) # Capture one or more numbers
)     # Close the group
+     # Group should appear one or more times
x?    # The final group may have an x directly after it
\s*   # Zero or more trailing spaces are allowed
$     # Match the end of the string

编辑捕获数字,不确定您是否要执行此操作或仅匹配字符串。

答案 3 :(得分:0)

这对我有用

\d*x\s*$|\d*[^x] \d[^\s]*

答案 4 :(得分:0)

我新版了一行红宝石,所以下面的语法可能是可见的

但是,解决问题的最简单方法是:首先将第一个案例减少到第二个案例,然后匹配数字。

喜欢的东西;

("20x 30".gsub/^\s*(\d+)x\s*$/,'\1').match(/\b\d+\b/)