str_extract特定模式(示例)

用户名

我仍然对正则表达式语法有些困惑。您能否以这些模式帮助我:

_A00_A1234B_
_A00_A12345B_
_A1_A12345_

到目前为止,我的方法:

vapply(strsplit(files, "[_.]"), function(files) files[nchar(files) == 7][1], character(1))

或者

str_extract(str2, "[A-Z][0-9]{5}[A-Z]")

预期的产出是

A1234B
A12345B
A12345

谢谢!

阿克伦

你可以试试

library(stringr)
str_extract(str2, "[A-Z][0-9]{4,5}[A-Z]?")
#[1] "A1234B"  "A12345B" "A12345" 

在此模式中,查找大写字母[A-Z],后跟4或5位数字[0-9]{4,5},后跟大写字母[A-Z] ?

或者您可以使用stringi更快

library(stringi)
 stri_extract(str2, regex="[A-Z][0-9]{4,5}[A-Z]?")
 #[1] "A1234B"  "A12345B" "A12345" 

或一个base R选择是

 regmatches(str2,regexpr('[A-Z][0-9]{4,5}[A-Z]?', str2))
 #[1] "A1234B"  "A12345B" "A12345" 

数据

str2 <- c('_A00_A1234B_', '_A00_A12345B_', '_A1_A12345_')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

str_extract特定模式

R-如何在循环中使字符串str_extract模式参数化

Str_extract不起作用

str_extract 所有语法

如何为r中的str_extract提供多个条件?

如何用底数R做str_extract?

str_extract()和summarise()给我没有行

stringr::str_extract 列表 R 的所有元素

str_extract()提供了不同的结果,从数据帧调用矢量-R

为什么`str_extract`只捕获其中一些值?

使用str_extract在两个方括号之间选择文本

斯特林,str_extract:如何做正向后看?

str_extract仅捕获一个重复出现的关键字实例

字符串str_extract捕获组,捕获所有内容

str_extract:从字符串中提取第n个单词

将str_extract与perl结合使用以提取子字符串

R使用str_extract(stringr)导出“ _”之间的字符串

减去子列表并使用str_extract函数时如何使用lapply

如何从str_extract中提取确切的字符串?

str_extract 生成 NA,同时使用 mutate 删除开头的子字符串

帶引號和分號的 str_extract 正則表達式

忽略str_extract中字母数字字符的可选组合

如何使用str_extract提取两个单词之间的文本?

如何从包stringr中使用str_extract()提取括号中的数字(但不包括括号)?

使用R,在这种情况下如何正确使用str_extract?

将参数传递到包含带有stringr :: str_extract的dplyr管道表达式group_by的函数

str。包含大熊猫的特定模式

QT MVC模式不更新视图-特定的SimpleTreeModel示例

在R中使用str_extract在带有正则表达式的子字符串之前提取数字