在字符串中查找字符的位置

96
我想查找字符串中字符的位置。例如: string =“the2quickbrownfoxeswere2tired” 。我希望函数返回 2 的位置,即字符串2的字符位置为424

为什么要使用正则表达式?r 没有 .indexOf() 或其他类似的函数吗? - fge
2
我有所怀疑。开发者们是Nixers,假设每个人都知道正则表达式。 R 的字符串处理有点笨拙。 - IRTFM
6个回答

128
您可以使用gregexpr
 gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired")


[[1]]
[1]  4 24
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE

或者使用包含在stringr包中的str_locate_all函数,它是gregexprstringi::stri_locate_all的一个封装(从stringr版本1.0开始)

library(stringr)
str_locate_all(pattern ='2', "the2quickbrownfoxeswere2tired")

[[1]]
     start end
[1,]     4   4
[2,]    24  24

请注意,您可以简单地使用 stringi

library(stringi)
stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)

在基本的R中,另一个选项可能是类似于以下内容:

lapply(strsplit(x, ''), function(x) which(x == '2'))

应该可以工作(假设给定一个字符向量x


我们如何从您前三个解决方案返回的列表/对象中提取整数? - 3pitt
使用regexpr而不是gregexpr可以轻松获取整数。或者按照下面另一个答案中所示,在输出上使用unlist - Arani

45

这是另一种直接的选择。

> which(strsplit(string, "")[[1]]=="2")
[1]  4 24

你能解释一下 [[1]] 是什么意思吗? - francoiskroll
@francoiskroll,[[1]]代表列表的第一个元素。 - Prafulla

22

您可以使用unlist将输出设置为4和24:

unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired"))
[1]  4 24

4

查找str2在str1中第n个出现的位置(参数顺序与Oracle SQL INSTR相同),如果没有找到则返回0

instr <- function(str1,str2,startpos=1,n=1){
    aa=unlist(strsplit(substring(str1,startpos),str2))
    if(length(aa) < n+1 ) return(0);
    return(sum(nchar(aa[1:n])) + startpos+(n-1)*nchar(str2) )
}


instr('xxabcdefabdddfabx','ab')
[1] 3
instr('xxabcdefabdddfabx','ab',1,3)
[1] 15
instr('xxabcdefabdddfabx','xx',2,1)
[1] 0

2
你也可以使用 grep 命令:
grep('2', strsplit(string, '')[[1]])
#4 24

2

要仅找到第一个位置,请使用lapply()min()

最初的回答

my_string <- c("test1", "test1test1", "test1test1test1")

unlist(lapply(gregexpr(pattern = '1', my_string), min))
#> [1] 5 5 5

# or the readable tidyverse form
my_string %>%
  gregexpr(pattern = '1') %>%
  lapply(min) %>%
  unlist()
#> [1] 5 5 5

要仅查找最后的位置,请使用lapply()max()

最初的回答:

unlist(lapply(gregexpr(pattern = '1', my_string), max))
#> [1]  5 10 15

# or the readable tidyverse form
my_string %>%
  gregexpr(pattern = '1') %>%
  lapply(max) %>%
  unlist()
#> [1]  5 10 15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接