My input is like:
tag.service
Where tag.
is optional but service
is mandatory (though either could be any length or value including word, digit or -
). If tag is present it will always be delaminated from service via .
Some more example inputs:
abc.def
a12.34b
1-2.3-4
123
abc
1-2
I'm trying to extract tag and service to two separate groups where tag must be group 1 and service must be group 2 (even if tag is not present).
I've managed to get this far:
^([\d\w]*)\.([\d\w]*)$
However the above doesn't allow group one to be optional and doesn't ensure group 2 is always service.
I think the below might get me a bit closer but is still fairly far off:
^(?:([\d\w]*)\.)?([\d\w]*)$
The engine used is based on RE2 and unfortunately there is not option other than REGEX for this i.e. I'm unable to inspect the groups post REGEX to establish which group has which value.
Any help much appreciated!
I'm not familiar with re2 but the following option only uses regex features you are using in your question, matches all examples and always puts the tag (if present) into the first group and the service into the second:
/^(?:([a-z0-9-]+)\.)?([a-z0-9-]+)$/
Demo here: https://regexr.com/48lnl