简体   繁体   中英

Using R Regex to identify two characters followed by a dash and two numbers

Very obnoxious regex question incoming. I have a column that I am trying to split into two based off a condition, I'd like a new column to be created when there are two characters. followed by a dash and two numbers (eg,, CA-01 ).

My code is:

mydf %>% extract(col = pilot_id, regex = "[az]{2}.d{2}", into = 'facility_test')

Where the column I'd like to identify the pattern in is pilot_id , and the new column I'd like to make is facility_test .

We need to capture in extract

library(dplyr)
library(tidyr)
mydf %>%
  extract(col = pilot_id,  regex = ".*-([A-Z]{2}-\\d{2})\\s.*", 
     into = 'facility_test')

# A tibble: 1 x 1
#    facility_test
#  <chr>        
#1 FL-03       

data

mydf <- tibble(pilot_id = "TGT Track -FL-03 (Hilsborough County) 3/3/2021")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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