简体   繁体   中英

replacing all behind my matched string - gsub

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?

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM