str_extract specific patterns (example)

前端 未结 4 1859
轻奢々
轻奢々 2021-01-06 12:04

I\'m still a little confused by regex syntax. Can you please help me with these patterns:

_A00_A1234B_
_A00_A12345B_
_A1_A12345_

my approac

4条回答
  •  暖寄归人
    2021-01-06 12:45

    Using rex to construct the regular expression may make it more understandable.

    x <- c("_A00_A1234B_", "_A00_A12345B_", "_A1_A12345_")
    
    # approach #1, assumes always is between the second underscores.
    re_matches(x,
      rex(
        "_",
        anything,
        "_",
        capture(anything),
        "_"
      )
    )
    
    #>         1
    #> 1  A1234B
    #> 2 A12345B
    #> 3  A12345
    
    
    # approach #2, assumes an alpha, followed by 4 or 5 digits with a possible trailing alpha.
    re_matches(x,
      rex(
        capture(
          alpha,
          between(digit, 4, 5),
          maybe(alpha)
        )
      )
    )
    
    #>         1
    #> 1  A1234B
    #> 2 A12345B
    #> 3  A12345
    

提交回复
热议问题