Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我很难找到解决正则表达式问题的方法。我想从字符串的开头匹配 G06 的所有内容,但不包括 G06Q。我猜它是某种向后看?
mystring <- c('G06', 'G06Q', 'G11C')
我想要的是
[1] "G06"
作为初学者,我尝试关注,但显然需要一些额外的部分
grep("^G06", mystring, value=TRUE, perl=TRUE)给我
grep("^G06", mystring, value=TRUE, perl=TRUE)
[1] "G06" "G06Q"
可以在此处找到类似的问题Link但我很难将它用于我手头的问题。
我们可以指定$字符串的结尾()
$
grep("^G06$", mystring, value=TRUE, perl=TRUE) #[1] "G06"
或者使用单词边界(\\b)来标记单词的结尾
\\b
grep("^G06\\b", mystring, value=TRUE, perl=TRUE) #[1] "G06"