[英]Grep for a regular expression in R
I have a vector of character strings which I need to grep through. 我有一个字符串向量,需要grep通过。
The term I would like to grep is "A-10", but I would like it only to pick up rows where "A-10" is an independent word (eg "A-10 aircraft maintenance" and NOT "WQDA-10-ASP"). 我想用grep表示的术语是“ A-10”,但我只想选择“ A-10”是一个独立词的行(例如,“ A-10飞机维修”而不是“ WQDA-10-” ASP”)。
Which regular expression(s) would allow me to grep for "A-10" as an separate word and not part of a different word or string? 哪个正则表达式可以让我将“ A-10”作为单独的单词而不是其他单词或字符串的一部分进行grep?
How about this: 这个怎么样:
abc <- c('A-10 maintanance', 'WQDA-10-ASP')
grep('(^|\\s)A-10($|\\s)', abc)
where (^|\\\\s)
means beginning of string or whitespace, and ($|\\\\s)
means end of line or whitespace 其中(^|\\\\s)
表示字符串或空白的开始,而($|\\\\s)
表示行或空白的结束
Also take a look at the stringr
package if you want some nice regex functions. 如果您想要一些不错的regex函数,也请看一下stringr
包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.