-3

I have an issue with a regex. here below details:

I have the following basis chain:
N_aa-AAAA-140143-AAAAAA_1---

The regex i'm using:

^N_([A-Za-z]+)-([A-Za-z]+)-([A-Za-z0-9]+)-([A-Za-z0-9_-éÉ\s]+)---

The result i have for the basis chain:
group1: aa.
group2: AAAA.
group3: 140143.
group4: AAAAAA_1.

But sometimes there is some missing element between "--" for the group3:
N_AA-AA--AAAA_1---

The result i would like in this case:
group1: aa.
group2: AAAA.
group3: blank (nothing).
group4: AAAAAA_1.

Thanks for your help ;-).

whands
  • 21
  • 7

1 Answers1

0

If the 3rd group is optional then use the * quantifier (0 or many occurrences) instead of + (at least 1 or many occurrences):

^N_([A-Za-z]+)-([A-Za-z]+)-([A-Za-z0-9]*)-([A-Za-z0-9_-éÉ\s]+)---

See it passing your 2 test-cases here: https://regex101.com/r/DWStTX/1

hc_dev
  • 8,389
  • 1
  • 26
  • 38