2

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?

0

1 Answer 1

2

This is very likely a dup, but here's an answer: You want to capture the parts to keep, not the whole thing. So you want two capture groups (the years), and the replacement should include them using "\1" and "\2". That is,

> text_to_change <- "__1990_1999__extra_text_199_199"
> gsub('([0-9]{4})_([0-9]{4})','\\1-\\2',text_to_change)
[1] "__1990-1999__extra_text_199_199"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.