Ho to do this using Regex?

Viewed 40

I'm trying to create a regex code that matches the folowing pattern :

{format:Tx.(string_of_date_format)}

where Tx can be T-n T-1 T0 T1 T2 Tn and string_of_date_format could be any string that represents a date format yyyyMMdd as example this is my implementation which I couldn't figure out why it didn't match

{format:^T(-?[0-9]+)([+-][0-9]+)?$[.]([a-zA-Z])}

so I can use it like in this example: {format:T2.(yyyyMMdd)}

1 Answers

You can use

{format:T-?[0-9]+(?:[+-][0-9]+)?\.\([a-zA-Z]+\)}

See the regex demo. Details:

  • {format:T - a literal string
  • -? - an optional -
  • [0-9]+ - one or more digits
  • (?:[+-][0-9]+)? - an optional occurrence of + or - and then one or more digits
  • \. - a dot
  • \( - a ( char
  • [a-zA-Z]+ - one or more letters
  • \)} - )} substring.
Related