0

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?

splash
  • 13,037
  • 1
  • 44
  • 67
Abir Chokraborty
  • 1,695
  • 4
  • 15
  • 23
  • A simple regex to match for the date will be `/[0-9]{4}-[0-9]{2}-[0-9]{2}/` that will match any numbers in the format 4digit-2digit-2digit which also include invalid dates like `0000-99-99`. – Rabin May 14 '17 at 09:22
  • Now I can capture `"1999-08-16"` using `\"[0-9]{4}-[0-9]{2}-[0-9]{2}\"`. But if I want to capture the whole string as `"1999-08-16"^^xsd:date` then what will be the regex? I have tried with `\"[0-9]{4}-[0-9]{2}-[0-9]{2}\"\^\^"xsd:date"` but not working. – Abir Chokraborty May 14 '17 at 09:47

2 Answers2

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
  • (?:0?[1-9]|1[012]) - month, can be:
    • 01-09 or 1-9 (that's why 0?)
    • 10,11,12 (1[012] part)
  • ?: 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)
    • 1-9, or 01-09 (0?[1-9])
      Also we use non-capturing group for matching day
  • $ 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:

(?: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:

  • (?:\d{2}|\d) gives 2 matches
  • (?:\d|\d{2}) gives 3 matches
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
0

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

Abir Chokraborty
  • 1,695
  • 4
  • 15
  • 23