Reputation: 2703
I'm fairly new to string manipulations in R. I have a case in which I'm using several matched replacements in a foor loop, and thus has to rely on gsub.
Now I have a string (illustrative example), "Today is a great Day"
In which I want to use the pattern "Today is" only, and replace it with "My value"
But what is the metacharacters I need to select the rest of the string?
My try
gsub("Today is+.", "My value", myobject)
Now this only selects one value after "Today is", how can I make it run all the way?
Upvotes: 1
Views: 162
Reputation: 626835
Note that if the rest of the string contains newline characters, the greedy dot matching will only return the rest of the line.
In order to match newlines, too, you will need to use [\\s\\S]*
, or a Perl-style (?s)
inline modifier with lazy dot matching:
gsub("Today is([\\s\\S]*)", "My value\\1", x)
or
gsub("(?s)Today is(.*)", "My value\\1", x, perl=T)
Note that there is no need to put known, literal text into a capture group, it is redundant overhead.
See IDEONE demo
Upvotes: 1
Reputation: 263342
Use a capture-class grouping with parens in the pattern, and refer back to them with \\<n>
in the replacement, and I think you need to swap the order of .+
in the pattern:
> gsub("(Today is)(.+)", "My value\\2", "Today is a great Day")
[1] "My value a great Day"
Upvotes: 5