Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

  • "Text" should return "Text"
  • "Sample-Text" should return "Text"

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help would be greatly appreciated!

>Solution :

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

[^-]*$

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading