如何在R中精确匹配正则表达式并拉出模式

M. *_*wik 2 regex r

我想从我的字符串向量中获取模式

string <- c(
  "P10000101 - Przychody netto ze sprzedazy produktów" ,                    
  "P10000102_PL - Przychody nettozy uslug",                     
  "P1000010201_PL - Handlowych, marketingowych, szkoleniowych",             
  "P100001020101 - - Handlowych,, szkoleniowych - refaktury",
  "- Handlowych, marketingowych,P100001020102, - pozostale"
)
Run Code Online (Sandbox Code Playgroud)

结果我想得到正则表达式的精确匹配

result <- c(
  "P10000101",
  "P10000102_PL",
  "P1000010201_PL",
  "P100001020101",
  "P100001020102"
)
Run Code Online (Sandbox Code Playgroud)

我试过这个pattern = "([PLA]\\d+)"和不同的组合value = T, fixed = T, perl = T.

grep(x = string, pattern = "([PLA]\\d+(_PL)?)", fixed = T)
Run Code Online (Sandbox Code Playgroud)

akr*_*run 6

我们可以试试 str_extract

library(stringr)
str_extract(string, "P\\d+(_[A-Z]+)*")
#[1] "P10000101"      "P10000102_PL"   "P1000010201_PL" "P100001020101"  "P100001020102" 
Run Code Online (Sandbox Code Playgroud)

grep用于查找匹配模式是否存在于特定字符串中.提取时,使用subgregexpr/regmatchesstr_extract

使用base R(regexpr/regmatches)

regmatches(string, regexpr("P\\d+(_[A-Z]+)*", string))
#[1] "P10000101"      "P10000102_PL"   "P1000010201_PL" "P100001020101"  "P100001020102" 
Run Code Online (Sandbox Code Playgroud)

基本上,匹配的模式P后跟一个数字(\\d+)后跟greedy(*)匹配_和一个或多个大写字母.

  • `R`的开发版本将使用`strcapture`对此进行原生支持.请参阅:https://stat.ethz.ch/R-manual/R-devel/library/utils/html/strcapture.html并:https: //cran.r-project.org/doc/manuals/r-devel/NEWS.html (4认同)
  • 你可以在`_`之前删除````,因为`_`是一个"word"字符. (2认同)