与日期格式匹配的RegEx表达式

时间:2022-03-23 09:40:30

I have a line with "1999-08-16"^^xsd:date. What will be the regex to capture the whole string as "1999-08-16"^^xsd:datein flex file? And, is it possible to capture only "1999-08-16" as a string. If yes, then what will be the regex for it in flex?

我有一行“1999-08-16”^^ xsd:date。什么是正则表达式捕获整个字符串为“1999-08-16”^^ xsd:datein flex文件?并且,是否可以仅将“1999-08-16”捕获为字符串。如果是,那么flex中的正则表达式是什么?

2 个解决方案

#1


0  

Try this one:

试试这个:

^"(\d{4}-(?:0?[1-9]|1[012])-(?:30|31|[12]\d|0?[1-9]))"\^\^xsd:date$

Explanation:

  • ^ - start of line
  • ^ - 开始行

  • \d{4} - year part
  • \ d {4} - 年份

  • (?:0?[1-9]|1[012]) - month, can be:
    • 01-09 or 1-9 (that's why 0?)
    • 01-09或1-9(这就是为什么0?)

    • 10,11,12 (1[012] part)
    • 10,11,12(1 [012]部分)

  • (?:0?[1-9] | 1 [012]) - 月,可以是:01-09或1-9(这就是为什么0?)10,11,12(1 [012]部分)

  • ?: means non-capturing group (if we need just alternating matches with |, but not outputting them to user)
  • ?:表示非捕获组(如果我们只需要与|交替匹配,但不将它们输出给用户)

  • (?:30|31|[12]\d|0?[1-9]) - day part, can be:
    • 30,31,
    • 10-29 (part [12]\d)
    • 10-29(部分[12] \ d)

    • 1-9, or 01-09 (0?[1-9])
      Also we use non-capturing group for matching day
    • 1-9,或01-09(0?[1-9])我们也使用非捕获组进行匹配日

  • (?:30 | 31 | [12] \ d | 0?[1-9]) - 日期部分,可以是:30,31,10-29(部分[12] \ d)1-9,或01- 09(0?[1-9])我们也使用非捕获组进行匹配

  • $ matches end of line
  • $匹配行尾

  • Everything in between "" is captured to standard capturing group, as you need to extract date
  • “”之间的所有内容都会被捕获到标准捕获组,因为您需要提取日期

Demo

NOTICE: When matching days we put 1-9 day numbers in LAST alternating group:

注意:在匹配天数时,我们在最后一个交替组中放置1-9天数字:

(?:30|31|[12]\d|0?[1-9])

that's because regex engine when given alternating matches uses FIRST matched result and other matched alternatives are ignored. For example- in string 1 11 expression:

这是因为给定交替匹配时的正则表达式引擎使用FIRST匹配结果,其他匹配的替代结果将被忽略。例如 - 在字符串1 11表达式中:

  • (?:\d{2}|\d) gives 2 matches
  • (?:\ d {2} | \ d)给出2个匹配

  • (?:\d|\d{2}) gives 3 matches
  • (?:\ d | \ d {2})给出了3场比赛

#2


0  

To capture whole string \"[0-9]{4}-[0-9]{2}-[0-9]{2}\"\^\^[^ ]* can be used.

要捕获整个字符串\“[0-9] {4} - [0-9] {2} - [0-9] {2} \”\ ^ \ ^ [^] *可以使用。

#1


0  

Try this one:

试试这个:

^"(\d{4}-(?:0?[1-9]|1[012])-(?:30|31|[12]\d|0?[1-9]))"\^\^xsd:date$

Explanation:

  • ^ - start of line
  • ^ - 开始行

  • \d{4} - year part
  • \ d {4} - 年份

  • (?:0?[1-9]|1[012]) - month, can be:
    • 01-09 or 1-9 (that's why 0?)
    • 01-09或1-9(这就是为什么0?)

    • 10,11,12 (1[012] part)
    • 10,11,12(1 [012]部分)

  • (?:0?[1-9] | 1 [012]) - 月,可以是:01-09或1-9(这就是为什么0?)10,11,12(1 [012]部分)

  • ?: means non-capturing group (if we need just alternating matches with |, but not outputting them to user)
  • ?:表示非捕获组(如果我们只需要与|交替匹配,但不将它们输出给用户)

  • (?:30|31|[12]\d|0?[1-9]) - day part, can be:
    • 30,31,
    • 10-29 (part [12]\d)
    • 10-29(部分[12] \ d)

    • 1-9, or 01-09 (0?[1-9])
      Also we use non-capturing group for matching day
    • 1-9,或01-09(0?[1-9])我们也使用非捕获组进行匹配日

  • (?:30 | 31 | [12] \ d | 0?[1-9]) - 日期部分,可以是:30,31,10-29(部分[12] \ d)1-9,或01- 09(0?[1-9])我们也使用非捕获组进行匹配

  • $ matches end of line
  • $匹配行尾

  • Everything in between "" is captured to standard capturing group, as you need to extract date
  • “”之间的所有内容都会被捕获到标准捕获组,因为您需要提取日期

Demo

NOTICE: When matching days we put 1-9 day numbers in LAST alternating group:

注意:在匹配天数时,我们在最后一个交替组中放置1-9天数字:

(?:30|31|[12]\d|0?[1-9])

that's because regex engine when given alternating matches uses FIRST matched result and other matched alternatives are ignored. For example- in string 1 11 expression:

这是因为给定交替匹配时的正则表达式引擎使用FIRST匹配结果,其他匹配的替代结果将被忽略。例如 - 在字符串1 11表达式中:

  • (?:\d{2}|\d) gives 2 matches
  • (?:\ d {2} | \ d)给出2个匹配

  • (?:\d|\d{2}) gives 3 matches
  • (?:\ d | \ d {2})给出了3场比赛

#2


0  

To capture whole string \"[0-9]{4}-[0-9]{2}-[0-9]{2}\"\^\^[^ ]* can be used.

要捕获整个字符串\“[0-9] {4} - [0-9] {2} - [0-9] {2} \”\ ^ \ ^ [^] *可以使用。