简体   繁体   English

从字符串中提取特定值

[英]extracting a specific value from a string

Suppose I have a list of string:假设我有一个字符串列表:

distance <- c("CHI #12 DEBRINCAT(1), Snap, Off. Zone, 18 ft.Assists: #88 KANE(2); #56 GUSTAFSSON(1)", "TOR ONGOAL - #44 RIELLY, Backhand, Off. Zone, 77 ft.")

Now I hope to get a string vector that contains only the parts that contains the distance, that is, substring = c ("18 ft", "77 ft").现在我希望得到一个只包含包含距离的部分的字符串向量,即substring = c("18 ft", "77 ft")。

Is there a convenient way in R to do this? R 中是否有方便的方法来执行此操作?

Using str_extract to match one or more digits followed by zero or more spaces ( \\s* ) and the substring 'ft'使用str_extract匹配一个或多个数字后跟零个或多个空格 ( \\s* ) 和 substring 'ft'

library(stringr)
str_extract(distance, "\\d+\\s*ft")
#[1] "18 ft" "77 ft"

Alternatives:备择方案:

regmatches(distance, gregexpr("\\b[0-9]+\\s*ft", distance, perl = TRUE))
# [[1]]
# [1] "18 ft"
# [[2]]
# [1] "77 ft"

strcapture("\\b([0-9]+\\s*ft)", distance, list(dist = ""))
#    dist
# 1 18 ft
# 2 77 ft

Though they're all just doing the same thing with slightly different interfaces.尽管他们都只是在做同样的事情,但界面略有不同。

Try gsub试试gsub

> gsub(".*?(\\d+\\s+ft).*", "\\1", distance)
[1] "18 ft" "77 ft"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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