Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp : any chr but ignore word

Trying to capture server names from string.

A server name can be

  • letters + digits
  • letters + digits + letters (but not 'root')

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

  • app000123

Or

  • app000123sql

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)

enter image description here

like image 347
SimonB Avatar asked Oct 24 '25 02:10

SimonB


1 Answers

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 lookahead

See 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.

like image 177
The fourth bird Avatar answered Oct 25 '25 17:10

The fourth bird