so_as
so_as

Reputation: 41

Regex to not match the dashes or a prefix in a string

I have various strings in the format of .PREFIX-word1-word2-word3 where PREFIX can be any word and there can be 1 or more words that follow the prefix (in this case word1 word2 word3).

What I'm attempting to do is match on any words that follow the prefix so that in the end I get back just the words (in this case word1 word2 word3). Therefore, I don't want to match on .PREFIX or any dashes.

Given that PREFIX can be anything I've been using (?<=-).*$ which matches word1-word2-word3.

What am I missing in order to just match on word1 word2 word3?

Upvotes: 1

Views: 941

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

You can exclude matching the hyphen using a negated character class starting with [^

(?<=-)[^-]+

Regex demo

If you just want a single word after the hyphen, you can also exclude matching whitespace chars \s

(?<=-)[^-\s]+

Regex demo

Upvotes: 1

Related Questions