Home > Software design >  Conditional whitespace in output of regex replacement
Conditional whitespace in output of regex replacement

Time:08-15

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.

CodePudding user response:

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

  • Related