简体   繁体   中英

Grep for a regular expression in R

I have a vector of character strings which I need to grep through.

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").

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?

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

Also take a look at the stringr package if you want some nice regex functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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