Trying to capture server names from string.
A server name can be
Problem is that in circumstances the word 'root' gets added to the end of the string by the data source.
ab-vol-bapp000123-use-dev
ab-vol-bapp000123sql-use-dev
ab-vol-bapp000123root-use-dev
ab-vol-bapp000123sqlroot-use-dev
In the above cases, I need to get either
Or
However, struggling to capture the chrs after the digits whilst ignoring/excluding 'root'
This is my best attempt:
(^ab-vol-) # literal
([a-z]{2,4}) # 2-4 alphas
([0-9]{4,6}) # 4-6 numerics
(
(?!root) # ignore 'root'
[a-z]{0,4} # 0-4 alphas
)?
Obviously my "ignore 'root'" is not doing as described (last test line below fails), and I can see why - I just don't know what the alternative answer is ðŸ˜
Appreciate any guidance! Thanks
(Notes :Working in AWS redshift)

What you might do is match as least as possible 0-4 chars and assert that to the right is either the word "root" or a hyphen or the end of the string.
^(ab-vol-)([a-z]{2,4})([0-9]{4,6})([a-z]{0,4}?)(?=root\b|-|$)
The pattern matches
^ Start of string(ab-vol-) Capture the literal text([a-z]{2,4}) Capture 2-4 chars a-z([0-9]{4,6}) Capture 4-6 digits([a-z]{0,4}?) Capture 0-4 times a char a-z, as least as possible(?= Positive lookahead, assert the to the right of the current position is
root\b|-|$ Match either the word root or a hyphen or assert the end of the string) Close the lookaheadSee a regex demo.
If you just want to match all chars that are not followed by the word "root", you could match all chars a-z except for r, and then only match r when not directly followed by oot and a word boundary.
(^ab-vol-)([a-z]{2,4})([0-9]{4,6})([a-qs-z]*(?:r(?!oot\b)[a-qs-z]*)*)
See a regex demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With