简体   繁体   中英

How to extract words between two periods using gsub

I have a text that looks like this:

txt <- "w.raw.median"

I want to extract the second word in between two periods ( . ), giving this output

> raw

But why this doesn't work

gsub(".*\\.", "", txt)

What's the right way to do it?

Try this:

gsub(".*\\.(.*)\\..*", "\\1", txt)
[1] "raw"

Also consider

strsplit(txt,'.',fixed=TRUE)[[1]][2]

for a (slightly) more readable version

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