0

私は正規表現に非常に慣れていないので、どこから始めればよいかわかりません。時間範囲の値をファイルに保存する必要があります。保存する前に、正規表現に対して検証したいと思います。

フォーマットは次のとおりです。

00:00-00:00

ここで、桁の最小値は 0 で、最大値は次のように表すことができます。

23:59-23:59

文字列がこの制約に一致することを確認するにはどうすればよいですか?

4

1 に答える 1

0

これを試して

\b((?:(?:[01][0-9]|2[0-3]):(?:[0-5][0-9]))-(?:(?:[01][0-9]|2[0-3]):(?:[0-5][0-9])))\b

説明

\b                   # Assert position at a word boundary
(                    # Match the regular expression below and capture its match into backreference number 1
   (?:                  # Match the regular expression below
      (?:                  # Match the regular expression below
                              # Match either the regular expression below (attempting the next alternative only if this one fails)
            [01]                 # Match a single character present in the list “01”
            [0-9]                # Match a single character in the range between “0” and “9”
         |                    # Or match regular expression number 2 below (the entire group fails if this one fails to match)
            2                    # Match the character “2” literally
            [0-3]                # Match a single character in the range between “0” and “3”
      )
      :                    # Match the character “:” literally
      (?:                  # Match the regular expression below
         [0-5]                # Match a single character in the range between “0” and “5”
         [0-9]                # Match a single character in the range between “0” and “9”
      )
   )
   -                    # Match the character “-” literally
   (?:                  # Match the regular expression below
      (?:                  # Match the regular expression below
                              # Match either the regular expression below (attempting the next alternative only if this one fails)
            [01]                 # Match a single character present in the list “01”
            [0-9]                # Match a single character in the range between “0” and “9”
         |                    # Or match regular expression number 2 below (the entire group fails if this one fails to match)
            2                    # Match the character “2” literally
            [0-3]                # Match a single character in the range between “0” and “3”
      )
      :                    # Match the character “:” literally
      (?:                  # Match the regular expression below
         [0-5]                # Match a single character in the range between “0” and “5”
         [0-9]                # Match a single character in the range between “0” and “9”
      )
   )
)
\b                   # Assert position at a word boundary
于 2012-06-14T10:12:06.257 に答える