Nimo1981
Nimo1981

Reputation: 132

Finding regex between two - characters

Can someone help me extract the name out of this string.

status-task-testing-new-connector-0

I am trying to end up with testing-new-connector

I can get it to extract up to the end of connector so it ends up status-task-testing-new-connector using regex (?:[^-]*\-){4}([^-]*)

but I can't figure out how to move the selector forward as far as testing.

Upvotes: 0

Views: 37

Answers (1)

The fourth bird
The fourth bird

Reputation: 163352

Te pattern (?:[^-]-){4}([^-]) repeats the non capturing group 4 times and uses a capturing group for a single time matching not a -

As you are using a quantifier, you could also use a quantifier in the capturing group.

If there should be at least 1 char between, you could use + as a quantifier instead of * to prevent -- in the string.

^(?:[^-]+-){2}((?:[^-]+-){2}[^-]+)

Regex demo

Upvotes: 1

Related Questions