0

我正在寻找一个正则表达式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'
4

5 回答 5

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
----------------------------------------------------------------------
于 2013-09-05T13:13:07.187 回答
0

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

于 2013-09-05T13:07:28.723 回答
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

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

于 2013-09-05T13:13:46.623 回答
0

这对我有用

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

于 2013-09-05T13:32:17.203 回答
0

我更新了一行 ruby​​,所以下面的语法可能很糟糕

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

就像是;

("20x 30".gsub/^\s*(\d+)x\s*$/,'\1').match(/\b\d+\b/)
于 2013-09-05T13:53:07.647 回答