Suppose I have a data storage or filing system and I accept a few formats (legacy reasons, not my own design)
So for example, I need to accept
abcd.efgh.1234.4567
abcd-efgh-1234-4567
abcd|efgh|1234|4567
but not
abcd.efgh-1234|4567
Basically I need to be consistent about the delimiters I use. I am trying to construct a regex that can check that but I am finding it really tricky. I have explored regex references and see how they would work for finding repeats like abc-abc-abc
, but in my case I need it to allow the abcd
part to be different and only ensure I have the same delimeter
Here's what I've got so far (link to Regex101);
(([a-z1-9]){4}([\.:|])){3}(([a-z1-9]){4})
I need to somehow give a backreference to that ([\.:|])
but I can't put it in there since it needs to repeat on itself.
Is there anyway to do this in Regex?
You can capture the delimiter when it first appears, and then back reference it later:
See regex demo.
[a-z1-9]{4}
matches a length four word;([.:|])
matches and captures the delimiter;(?:[a-z1-9]{4}\1){2}
captures the second and third patterns, the delimiter is referred to as the delimiter captured above;[a-z1-9]{4}
matches the last word;