0

How can I incorporate these two regex patterns in one statement with conditions if the first three numbers are 123, then regex2 is applicable and if not equal to 123, then regex1 is applicable?

Regex1 "^C\sN\+\d{10,12}\/?(EN|FR)?$"
Value 1:  CTC N+6534567890/FR  

Regex2 "^C\sN\+\d{12}\/?(EN|FR)?$"           
Value 2:  CTC N+123456789012/FR
41686d6564
  • 15,043
  • 11
  • 32
  • 63
  • 1
    Shouldn't the `C` at the beginning of the pattern be `CTC`? – 41686d6564 Sep 16 '20 at 22:45
  • Since regex does backtracking on fail, a simple alternation works. `/123firstcase|secondcase` will match `123` followed by `firstcase` if there is `123`, then if ihat fails, it will try to match `secondcase`. – Amadan Sep 16 '20 at 22:45

1 Answers1

2

You may use:

^CTC\sN\+(?:123\d{9}|\d{10,12})\/?(EN|FR)?$

Demo.

The relevant part (i.e., (?:123\d{9}|\d{10,12})) will match either "123" followed by 9 digits (for a total of 12 in Regex2) or between 10 and 12 digits (from Regex1).

Note that unless you want to capture "EN" and "FR" separately, you may convert the capturing group into a non-capturing one (i.e., (?:EN|FR)).

One more thing to be aware of is that if the number starts with "123" and has a length <12 (10 or 11), it will still be a valid match (because it satisfies the \d{10,12} part from Regex1). If you need to prevent that (and assuming your regex flavor supports Lookaheads), you may use:

^CTC\sN\+(?:123\d{9}|(?!123)\d{10,12})\/?(EN|FR)?$

Demo.


References:

41686d6564
  • 15,043
  • 11
  • 32
  • 63
  • Thank you, it works... – Thirdy Leon Sep 16 '20 at 22:46
  • Hi @41686d6564 sorry but it works with >=12 digits if first 3 digits are 123 it should be fixed with 12 digits if starting with 123 if not starting with 123 it could be 10,11 and 12 digits... – Thirdy Leon Sep 16 '20 at 23:01
  • @Thirdy No, it works with _exactly 12 digits_ only (because `123` has a length of 3 and `\d{9}` matches exactly 9 digits). You can test it [here](https://regex101.com/r/Tr3mLP/2). Can you provide an example where it fails? – 41686d6564 Sep 16 '20 at 23:05
  • Hi 41686d6564 I've tried these values it matches CTC N+123456789012/FR;CTC N+12345678901/FR;CTC N+1234567890/FR, the last two values should be mismatched as they are 10 and 11 digits only the 12 digits should be matched unless otherwise they don't begin with 123... – Thirdy Leon Sep 16 '20 at 23:17
  • @ThirdyLeon Your previous comment said `>=12`, not `<=12`; that's why I was confused. Anyway, I've already thought of that and added another pattern to prevent it in case that's what you needed. Please see the updated answer. On another note, please edit the question and use the appropriate tag for the programming language (or regex flavor) that you're using. This helps to give you a more relevant answer because not all regex engines support the same features. – 41686d6564 Sep 16 '20 at 23:24
  • Hi @41686d6564, sorry for that... it works now for this expression ^CTC\sN\+(?:123\d{9}|(?!123)\d{10,12})\/?(EN|FR)?$. Thank you so much. – Thirdy Leon Sep 16 '20 at 23:39