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

Conditional whitespace in output of regex replacement

I have a list of music titles, and from the live tracks I want to extract the event and – if present – the year. The output must be formatted as event space year or just event. You can do this very easily with a regular expression match, evaluate the groups, and add a space when neccesary. But can you do this also with a simple replacement?

myResult=Regex.replace(myTitle,".*(event1|event2|event3)\D*(\d+)?.*", "$1 $2" ,RegexOptions.IgnoreCase)

This works but has a superfluous space when there is no year. Like I said, there are many ways to do this differently, but I wonder if you can do it with a single regex replacement. Note that the input string might have a year number but without a leading space.

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

>Solution :

If there is always at least a single space before the digits in group 2, you might write the pattern as:

.*(event1|event2|event3)(?:\D*?(\s\d+))?

And replace with $1$2 which would also prevent an empty space if group 2 is empty.

Regex demo

Or using $1 $2 in the replacement if there are no more digits following:

.*(event1|event2|event3)\D*?(\d*)\D*$

Regex demo

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