Caryon
Caryon

Reputation: 13

Regex to match everything right of a "-" but whole string if no "-" exists

I am trying to figure out a way to have a regex that returns the whole string, if no "-" is found in the string. But if there is a "-", it should only return everything to the right of that "-".

For example:

So far, i figured out how to solve the second part: (?<=-).* returns everything after a "-". However, i am completely stuck figuring out how to combine that and return everything if there is no "-".

Any help would be greatly appreciated!

Upvotes: 1

Views: 30

Answers (1)

blhsing
blhsing

Reputation: 106533

You can use a character set that excludes "-":

[^-]*$

Demo: https://regex101.com/r/yRm59e/1

Upvotes: 1

Related Questions