简体   繁体   中英

R case insensitive capturing group

This regex :

str_extract_all("This is a Test , ' ' " , "[a-z]+")

returns :

[1] "his" "is"  "a"   "est"

How to modify so this is case insensitive ?

`[1] "This" "is"  "a"   "Test"` 

should instead be returned

Should /i remove case sensitive ?

Trying str_extract_all("This is a Test , ' ' " , "[az]+/i")

returns

[[1]]
character(0)

There is a special notation for stringr functions :

regex(pattern, ignore_case = FALSE, multiline = FALSE, comments = FALSE, dotall = FALSE, ...)

You may use

> str_extract_all("This is a Test , ' ' " , regex("[a-z]+", ignore_case=TRUE))
[[1]]
[1] "This" "is"   "a"    "Test"

Alternatively, use an inline i modifier (?i) :

str_extract_all("This is a Test , ' ' " , "(?i)[a-z]+")

You could try including the capital letters in the set you're searching for.

str_extract_all("This is a Test , ' ' " , "[A-Za-z]+")

If you only want the first letter to be capitalized you could try the code below. It lets the first letter be case insensitive and then have only lowercase afterward.

str_extract_all("This is a Test , ' ' " , "[A-Za-z][a-z]*")

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