具有多个正则表达式的R字符串

问题描述 投票:0回答:3

我正在尝试使用多个pdf_ocr_text表达式过滤从regex创建的字符向量。具体来说,我想选择(1)以数字开头或(2)以两个空格和一个数字开头的元素。我也想在字符串中保留空格。这是一个可复制的示例。

df <- c("  065074                         10/1/91   10/1/96 8 10 5  ", 
"060227                          10/1/93   10/1/93 9 5 5  ", 
"  060178                  10/1/95   10/1/98 8 10 5  ", "060294                      10/1/91   10/1/98 8 10 5  ", 
"060212                 10/1/91   10/1/93 8 10 5   ", "  060228                   10/1/92   10/1/92 9 5 5  ", 
"  060257                        10/1/92   10/1/92 9 5 5   ", 
"060348                     10/1/91   10/1/93 8 10 5  ", "  080379                    10/1/91   10/1/96 6 20 5   ", 
"  060239                 10/1/91   10/1/98 8 10 5  ", "  060012                      10/1/92   10/1/92 9 5 5  ", 
"  060360                    10/1/96   10/1/96 9 5 5  ", "   060035                     10/1/95   10/1/95 9 5 5  ", 
"  060243                     10/1/92   10/1/93 8 10 5  ", "  060262                   10/1/92 ; 10/1/94 7 15 5  ", 
"            =          =          ", "                                    40097       2      4 40097 _"
)

我尝试了以下操作,但似乎不起作用。但是,如果我仅使用两个条件之一,则它可以工作。

df[df %>% str_detect(., "^\\s{2}\\d | ^\\d")]. # This fails
df[df %>% str_detect(., "^\\d")]. # With only one condition, it works
[1] "060227                          10/1/93   10/1/93 9 5 5  " "060294                      10/1/91   10/1/98 8 10 5  "   
[3] "060212                 10/1/91   10/1/93 8 10 5   "        "060348                     10/1/91   10/1/93 8 10 5  "    

如何使用两个正则表达式作为模式?

r regex stringr
3个回答
2
投票

使用现有方法,在管道字符周围放置空格:

df[df %>% str_detect("^\\s{2}\\d|^\\d")]

1
投票

使用grep

grep('^\\s{2}\\d|^\\d', df, value = TRUE)

# [1] "  065074                         10/1/91   10/1/96 8 10 5  "
# [2] "060227                          10/1/93   10/1/93 9 5 5  "  
# [3] "  060178                  10/1/95   10/1/98 8 10 5  "       
# [4] "060294                      10/1/91   10/1/98 8 10 5  "     
# [5] "060212                 10/1/91   10/1/93 8 10 5   "         
# [6] "  060228                   10/1/92   10/1/92 9 5 5  "       
# [7] "  060257                        10/1/92   10/1/92 9 5 5   " 
# [8] "060348                     10/1/91   10/1/93 8 10 5  "      
# [9] "  080379                    10/1/91   10/1/96 6 20 5   "    
#[10] "  060239                 10/1/91   10/1/98 8 10 5  "        
#[11] "  060012                      10/1/92   10/1/92 9 5 5  "    
#[12] "  060360                    10/1/96   10/1/96 9 5 5  "      
#[13] "  060243                     10/1/92   10/1/93 8 10 5  "    
#[14] "  060262                   10/1/92 ; 10/1/94 7 15 5  "      

或者,如果您喜欢stringr,则可以将str_subset用于相同的模式:

stringr::str_subset(df, '^\\s{2}\\d|^\\d')

您还可以将两个模式与可选的2个字符的空格组合在一起。

grep('^(\\s{2})?\\d', df, value = TRUE)

1
投票

此处尝试使用grep模式使用^\\s{2}?\\d

grep('^\\s{2}?\\d', df)

这里是正则表达式模式的说明:

^       from the start of the string
\s{2}?  match 2 spaces, zero or one times (read: match two spaces, or no spaces)
\d      match a single digit
© www.soinside.com 2019 - 2024. All rights reserved.