Ruby 需要正则表达式模式来匹配日期和可选时间

Ruby 需要正则表达式模式来匹配日期和可选时间,ruby,regex,datetime,Ruby,Regex,Datetime,我需要一个与日期和可选时间相匹配的正则表达式模式。 日期应为m/d/yyyy格式的有效美国日期。时间应为h:mm:ss am/pm或24小时hh:mm:ss 比赛日期:2011年9月1日| 2011年9月1日上午10:00 | 2011年9月1日上午10:00 | 2011年9月1日上午10:00 此模式将在RubyonRails项目中使用,因此它的格式应该可以通过Ruby使用。有关测试,请参阅 以下是我现有的约会模式(这可能是一种过度杀戮): 祝你好运 工作原理: " ^

我需要一个与日期和可选时间相匹配的正则表达式模式。

日期应为
m/d/yyyy
格式的有效美国日期。时间应为
h:mm:ss am/pm
或24小时
hh:mm:ss

比赛日期:2011年9月1日| 2011年9月1日上午10:00 | 2011年9月1日上午10:00 | 2011年9月1日上午10:00

此模式将在RubyonRails项目中使用,因此它的格式应该可以通过Ruby使用。有关测试,请参阅

以下是我现有的约会模式(这可能是一种过度杀戮):

祝你好运

工作原理:

    "
^                       # Assert position at the beginning of the string
(?:                     # Match the regular expression below
                           # Match either the regular expression below (attempting the next alternative only if this one fails)
      0                       # Match the character “0” literally
         ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]                   # Match a single character in the range between “1” and “9”
   |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      1                       # Match the character “1” literally
      [012]                   # Match a single character present in the list “012”
)
/                       # Match the character “/” literally
(?:                     # Match the regular expression below
                           # Match either the regular expression below (attempting the next alternative only if this one fails)
      0                       # Match the character “0” literally
         ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]                   # Match a single character in the range between “1” and “9”
   |                       # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      [12]                    # Match a single character present in the list “12”
      \d                      # Match a single digit 0..9
   |                       # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      3                       # Match the character “3” literally
      [01]                    # Match a single character present in the list “01”
)
/                       # Match the character “/” literally
(?:                     # Match the regular expression below
   \d                      # Match a single digit 0..9
      {4}                     # Exactly 4 times
)
(?:                     # Match the regular expression below
   \s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      +                       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:                     # Match the regular expression below
                              # Match either the regular expression below (attempting the next alternative only if this one fails)
         (?:                     # 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”
                  ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
               \d                      # Match a single digit 0..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”
            \d                      # Match a single digit 0..9
         )
      |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
         (?:                     # Match the regular expression below
                                    # Match either the regular expression below (attempting the next alternative only if this one fails)
               0                       # Match the character “0” literally
                  ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
               \d                      # Match a single digit 0..9
            |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
               1                       # Match the character “1” literally
               [0-2]                   # Match a single character in the range between “0” and “2”
         )
         :                       # Match the character “:” literally
         (?:                     # Match the regular expression below
            [0-5]                   # Match a single character in the range between “0” and “5”
            \d                      # Match a single digit 0..9
         )
         \s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
            +                       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
         [ap]                    # Match a single character present in the list “ap”
         m                       # Match the character “m” literally
   )
)?                      # Between zero and one times, as many times as possible, giving back as needed (greedy)
\s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *                       # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$                       # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
记住,弯曲的不是勺子,而是你

祝你好运

工作原理:

    "
^                       # Assert position at the beginning of the string
(?:                     # Match the regular expression below
                           # Match either the regular expression below (attempting the next alternative only if this one fails)
      0                       # Match the character “0” literally
         ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]                   # Match a single character in the range between “1” and “9”
   |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      1                       # Match the character “1” literally
      [012]                   # Match a single character present in the list “012”
)
/                       # Match the character “/” literally
(?:                     # Match the regular expression below
                           # Match either the regular expression below (attempting the next alternative only if this one fails)
      0                       # Match the character “0” literally
         ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]                   # Match a single character in the range between “1” and “9”
   |                       # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      [12]                    # Match a single character present in the list “12”
      \d                      # Match a single digit 0..9
   |                       # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      3                       # Match the character “3” literally
      [01]                    # Match a single character present in the list “01”
)
/                       # Match the character “/” literally
(?:                     # Match the regular expression below
   \d                      # Match a single digit 0..9
      {4}                     # Exactly 4 times
)
(?:                     # Match the regular expression below
   \s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      +                       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:                     # Match the regular expression below
                              # Match either the regular expression below (attempting the next alternative only if this one fails)
         (?:                     # 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”
                  ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
               \d                      # Match a single digit 0..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”
            \d                      # Match a single digit 0..9
         )
      |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
         (?:                     # Match the regular expression below
                                    # Match either the regular expression below (attempting the next alternative only if this one fails)
               0                       # Match the character “0” literally
                  ?                       # Between zero and one times, as many times as possible, giving back as needed (greedy)
               \d                      # Match a single digit 0..9
            |                       # Or match regular expression number 2 below (the entire group fails if this one fails to match)
               1                       # Match the character “1” literally
               [0-2]                   # Match a single character in the range between “0” and “2”
         )
         :                       # Match the character “:” literally
         (?:                     # Match the regular expression below
            [0-5]                   # Match a single character in the range between “0” and “5”
            \d                      # Match a single digit 0..9
         )
         \s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
            +                       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
         [ap]                    # Match a single character present in the list “ap”
         m                       # Match the character “m” literally
   )
)?                      # Between zero and one times, as many times as possible, giving back as needed (greedy)
\s                      # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *                       # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$                       # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

记住,弯曲的不是勺子,而是你

正则表达式对于这种工作来说是可怕的。如果您使用的是Ruby,我建议您使用来解析数据并检查其有效性:

def validate_date(date_str)
  valid_formats = ["%m/%d/%Y", "%m/%d/%Y %I:%M %P"] 
  #see http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime for more

  valid_formats.each do |format|
    valid = Time.strptime(date_str, format) rescue false

    return true if valid
  end

  return false
end

正则表达式对于这种工作来说是可怕的。如果您使用的是Ruby,我建议您使用来解析数据并检查其有效性:

def validate_date(date_str)
  valid_formats = ["%m/%d/%Y", "%m/%d/%Y %I:%M %P"] 
  #see http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime for more

  valid_formats.each do |format|
    valid = Time.strptime(date_str, format) rescue false

    return true if valid
  end

  return false
end

以下是我想到的似乎有效的方法:

regex=/^1?\d{1}/[123]?\d{1}/\d{4}(\s[12]?\d:[0-5]\d(:[0-5]\d)?(\s[ap]m)?$/
以下是我想到的似乎有效的方法:

regex=/^1?\d{1}/[123]?\d{1}/\d{4}(\s[12]?\d:[0-5]\d(:[0-5]\d)(\s[ap]m)?$/
好吧,这是我的结论;使用更严格的军事时间:

DATE_TIME_FORMAT = /^([0,1]?\d{1})\/([0-2]?\d{1}|[3][0,1]{1})\/([1]{1}[9]{1}[9]{1}\d{1}|[2-9]{1}\d{3})\s([0]?\d|1\d|2[0-3]):([0-5]\d):([0-5]\d)$/
比赛日期:2011年1月19日23:59:59

捕获:

  • 一,
  • 十九,
  • 2011年
  • 二十三
  • 59
  • 59

  • 好吧,这就是我的结局;使用更严格的军事时间:

    DATE_TIME_FORMAT = /^([0,1]?\d{1})\/([0-2]?\d{1}|[3][0,1]{1})\/([1]{1}[9]{1}[9]{1}\d{1}|[2-9]{1}\d{3})\s([0]?\d|1\d|2[0-3]):([0-5]\d):([0-5]\d)$/
    
    比赛日期:2011年1月19日23:59:59

    捕获:

  • 一,
  • 十九,
  • 2011年
  • 二十三
  • 59
  • 59

  • 如果你盯着它看的时间够长,它就是一艘帆船。如果你盯着它看的时间够长,它就是一艘帆船。嗯,你的方法似乎是一个有效的选择。然而,我正在处理现有的代码,这些代码被设置为使用正则表达式模式。基于以下内容的定制DSL:嗯,您的方法似乎是一个有效的替代方案。然而,我正在处理现有的代码,这些代码被设置为使用正则表达式模式。基于以下内容的自定义DSL: