简体   繁体   English

R不区分大小写的捕获组

[英]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 ? /i是否应该区分大小写?

Trying str_extract_all("This is a Test , ' ' " , "[az]+/i") 尝试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) : 或者,使用内联i修饰符(?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]*")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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