regex – How to use a regular expression in R as the replacement in gsub

I have file names with text in them that looks like “1999_2000” (year followed by underscore then year). The years change. I want to change the underscore to a hyphen (“-“). I’ve tried the following regular expression, including trying to capture the hyphen with parentheses.

text_to_change <- "__1990_1999__extra_text_199_199"
changed_text <- gsub('[0-9]{4}_[0-9]{4}','[0-9]{4}-[0-9]{4}',text_to_change)

> changed_text
[1] "__[0-9]{4}-[0-9]{4}__extra_text_199_199"

The result I want is:

"__1990-1999__extra_text_199_199"

I’ve tried to capture the underscore between the years using parentheses as in:

library(strex)
changed_text <- str_replace_all(text_to_change, "([0-9]{4}_[0-9]{4})", "[0-9]{4}-[0-9]{4}")

> changed_text
[1] "__[0-9]{4}-[0-9]{4}__extra_text_199_199"

Is there a way to replace the underscore between the years with a hyphen without hardcoding anything?

Read more here: Source link