Question

Hi guys I am new to the regex for some patterns matching i have wrote the regex but it is very lengthy regex so i was hoping that any one can help me optimize it ?

My REGEX-->

(T)(\d{9})(T)(\s{0,19}\d{0,19}\s{0,19}[0-9DO ]\d{0,19}\s{0,19}[OD0-9 ]\s{0,19}\d{0,19}\s{0,19}\d{0,19}[0-9D ]\d{0,19})(O)(\s{0,5}\d{0,19})\b

Pattern I want to match -->

T062102098T0741 0361212401O
T011900571T 000007250107O1274
T065000090T203 93 5797 9O 4037  
T325170628T0108626004D18O01054
T071108452T    42D896D5O 3587 
T041211609T 01D1270DD4O 2721
T021407912TO9196D00580D6O 7106
T081219108T O45D936D4O 3155
T074909988T O022D853 6O 1340

Pattern I am avoiding -->

T071904779T154505122619 2051
T065000090T000 32 493O9O 11007

So any help would be great .Thanks in advance.

Was it helpful?

Solution

Your RegEx is fine, rather it could be simplified (perhaps optimized) as below:

(?x)
   (
      (?:T\d{9})
      (?:T\s*\d{0,19}\s*[0-9DO ]\d{0,19}\s*[OD0-9 ]\s*\d{0,19}\s*\d{0,19}[0-9D ]\d{0,19})
      (?:O\s*\d{0,19})
      \b
   )

One thing I have found in your RegEx, is unnecessary use of (...). Moreover, using \s{0,19} or \s{0,5} could be simply written as \s*. The last one is better, only if you're not restricted to use of 0-19 times or 0-5 times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top