R中的一个字符串中包含多个regexpr
所以我有一个很长的字符串,我想处理多个匹配项。我似乎只能使用R中的一个字符串中包含多个regexpr,regex,r,Regex,R,所以我有一个很长的字符串,我想处理多个匹配项。我似乎只能使用regexpr获得第一个匹配的第一个位置。如何在同一字符串中获得多个位置(更多匹配) 我在html源代码中寻找一个特定的字符串。拍卖的价格(在html标记之间)。很难找到: 到目前为止,我使用的是: locationstart <- gregexpr("<span class=\"location-name\">", URL)[[1]]+28 locationend <- regexpr("<", subs
regexpr
获得第一个匹配的第一个位置。如何在同一字符串中获得多个位置(更多匹配)
我在html源代码中寻找一个特定的字符串。拍卖的价格(在html标记之间)。很难找到:
到目前为止,我使用的是:
locationstart <- gregexpr("<span class=\"location-name\">", URL)[[1]]+28
locationend <- regexpr("<", substring(URL, locationstart[1], locationend[1] + 100))
substring(URL, locationstart[1], locationstart[1] + locationend - 2)
locationstart使用gregexpr
允许多个匹配
> x <- c("only one match", "match1 and match2", "none here")
> m <- gregexpr("match[0-9]*", x)
> m
[[1]]
[1] 10
attr(,"match.length")
[1] 5
attr(,"useBytes")
[1] TRUE
[[2]]
[1] 1 12
attr(,"match.length")
[1] 6 6
attr(,"useBytes")
[1] TRUE
[[3]]
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE
Dason的回答中建议的gregexpr
和regmatches
允许在字符串中提取正则表达式模式的多个实例。此外,该解决方案的优点是完全依赖R的{base}
包,而不需要额外的包
尽管如此,我还是想根据实际情况提出另一种解决方案。一般来说,这个包通过提供base R的各种字符串支持函数(不仅仅是与正则表达式相关的函数)的大部分功能,以及一组直观命名的函数,并提供一致的API,使使用字符串变得更容易。事实上,stringr函数不仅取代了基本的R函数,而且在许多情况下引入了额外的特性;例如,stringr的正则表达式相关函数对于字符串和模式都是矢量化的
特别是对于在一个长字符串中提取多个模式的问题,可以使用str\u extract\u all
和str\u match\u all
,如下所示。根据输入是单个字符串或其向量这一事实,可以使用列表/矩阵下标、unlist
或其他方法(如lappy
、sapply
等)调整逻辑。关键是stringr函数返回的结构可用于访问我们想要的内容
# simulate html input. (Using bogus html tags to mark the target texts; the demo works
# the same for actual html patterns, the regular expression is just a bit more complex.
htmlInput <- paste("Lorem ipsum dolor<blah>MATCH_ONE<blah> sit amet, purus",
"sollicitudin<blah>MATCH2<blah>mauris, <blah>MATCH Nr 3<blah>vitae donec",
"risus ipsum, aenean quis, sapien",
"in lorem, condimentum ornare viverra",
"suscipit <blah>LAST MATCH<blah> ipsum eget ac. Non senectus",
"dolor mauris tellus, dui leo purus varius")
# str_extract() may need a bit of extra work to remove the leading and trailing parts
str_extract_all(htmlInput, "(<blah>)([^<]+)<")
# [[1]]
# [1] "<blah>MATCH_ONE<" "<blah>MATCH2<" "<blah>MATCH Nr 3<" "<blah>LAST MATCH<"
str_match_all(htmlInput, "<blah>([^<]+)<")[[1]][, 2]
# [1] "MATCH_ONE" "MATCH2" "MATCH Nr 3" "LAST MATCH"
#模拟html输入。(使用伪造的html标记来标记目标文本;演示可以正常工作
#与实际html模式相同,正则表达式只是稍微复杂一点。
htmlInput你能发布你的正则表达式吗??gregexpr
应该可以。@SimonO101,我想它的意思是全局
…?!@DavidStarkey添加了这个信息。太棒了。正是我想要的,还有更多(添加regmatches)。同样有趣的是,我只差一个字母。。。
# simulate html input. (Using bogus html tags to mark the target texts; the demo works
# the same for actual html patterns, the regular expression is just a bit more complex.
htmlInput <- paste("Lorem ipsum dolor<blah>MATCH_ONE<blah> sit amet, purus",
"sollicitudin<blah>MATCH2<blah>mauris, <blah>MATCH Nr 3<blah>vitae donec",
"risus ipsum, aenean quis, sapien",
"in lorem, condimentum ornare viverra",
"suscipit <blah>LAST MATCH<blah> ipsum eget ac. Non senectus",
"dolor mauris tellus, dui leo purus varius")
# str_extract() may need a bit of extra work to remove the leading and trailing parts
str_extract_all(htmlInput, "(<blah>)([^<]+)<")
# [[1]]
# [1] "<blah>MATCH_ONE<" "<blah>MATCH2<" "<blah>MATCH Nr 3<" "<blah>LAST MATCH<"
str_match_all(htmlInput, "<blah>([^<]+)<")[[1]][, 2]
# [1] "MATCH_ONE" "MATCH2" "MATCH Nr 3" "LAST MATCH"