How to fix this regex in order to get the mentioned outcome ?
regex: (0(?:[0-9]|[A-F]){3})(\"\*?(?:SIA-DCS|ADM-CID|NULL)\")(\d{4})(R?(?:[0-9]|[A-F])*)(L[^#]*)(#[^\[]*)(\[[^\[]*)(\[(?:M|V|P)(?:[^\[])*])
string: LCR005B"*ADM-CID"9876R579BDFL789ABC#12345A[4D32FC2B12345A|113002065][Vanydata][M1234567890AB][Panydata]
outcome should be:
- 005B
- “*ADM-CID”
- 9876
- R579BDF
- L789ABC
- #12345A
- [4D32FC2B12345A|113002065]
- [Vanydata]
- [M1234567890AB]
- [Panydata]
Later edit:
This currently outputs:
- 005B
- “*ADM-CID”
- 9876
- R579BDF
- L789ABC
- #12345A
- [4D32FC2B12345A|113002065]
- [Vanydata]
This is your regex:
You do have only 8 capturing groups, none is repeated, why do you think this should output 10 groups?
OK, Because of the
(?:M|V|P)of the last group, it would be able to match the content of the 3 last square brackets, but this group is not repeated, so it will match only the first one.You have 2 possibilities.
Put a quantifier behind the last group
or
this will now match the string till the end, but there are still only 8 capturing groups and the content of the last one is now not “[Vanydata]” anymore, its the last match of this group “[Panydata]”
Add two more groups to your regex
This does now have 10 capturing groups and the result is as you expected. If the Starting letter of those 3 last groups is always the same for each group you can simplify it to
See it here on Regexr
Update
You can make something optional by adding a question mark after it
See it here on Regexr, hovering over the match shows you the content of the capturing groups.