0

regexこれらのパターンのいずれかに一致する正規表現を探しています。

  • 番号に続くx
  • 1 つ以上のスペースで区切られた数値

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

これは私のために働く

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

于 2013-09-05T13:32:17.203 に答える