samuelt
samuelt

Reputation: 215

Regular Expressions to Extract Non-Standard String

My question has to do with using lookahead and lookbehind constructs in Regular Expressions with If-Then-Else Conditionals in combination with str_extract.

I have a string called UNIT in the table below that needs to be broken into its 3 component parts. The format is non-standard and I am using regex and str_extract to create new columns with each component.

I can easily extract the start (3A, 3C, etc.) and ends of the string (E, A), but the middle component is a bit more difficult. It can be 1-3 digits, or the two character code of SK, SD, or HH. I can use the code below individually, but the latter line overwrites the former.

So, my question is, how can I use the If-Then-Else Conditionals in Regular Expressions (?(?=regex)then|else) in combination with str_extract to get df2 from df1?

df1$C2 = str_extract(df1$UNIT,"(?<=[:upper:])\\d*(?<![:upper:])")

df1$C2 = str_extract(df1$UNIT, "S.$")


df1
ID  UNIT
1   3ASD
2   3C14E
3   3D5E
4   3E15E
5   3ESK
6   3B14A
7   3BHHQ2
8   3E101

df2
ID  UNIT    C1  C2  C3
1   3ASD    3A  SD  NA
2   3C14E   3C  14  E
3   3D5E    3D  5   E
4   3E15E   3E  15  E
5   3ESK    3E  SK  NA
6   3B14A   3B  14  A
7   3BHHQ2  3B  HH  Q2
8   3E101   3E  101 NA

Upvotes: 2

Views: 82

Answers (3)

Onyambu
Onyambu

Reputation: 79338

You can read in as a table:

 cbind(df1,read.table(text=sub("(..)(\\d+|SK|SD|HH)(.*)","\\1 \\2 \\3",df1$UNIT),fill=T,h=F,col.names = c("C1","C2","C3"),na.strings = ""))
  ID   UNIT C1  C2   C3
1  1   3ASD 3A  SD <NA>
2  2  3C14E 3C  14    E
3  3   3D5E 3D   5    E
4  4  3E15E 3E  15    E
5  5   3ESK 3E  SK <NA>
6  6  3B14A 3B  14    A
7  7 3BHHQ2 3B  HH   Q2
8  8  3E101 3E 101 <NA>

Upvotes: 1

samuelt
samuelt

Reputation: 215

Problem solved using the following code:

        df2$C1=  str_extract(df1$Unit, "^[:digit:][:upper:]")
    #if the start of the string is a digit and upper case letter then extract it into col C1

        df2$C2= str_extract(df1$Unit,"(?<=[:upper:])\\d*(?<![:upper:])|(?<=[:upper:])[[:upper:]][[:upper:]](?<=[:upper:])")
    #if a digit follows an uppercase letter or is behind another uppercase letter then extract all digits in between and extract it into C2
    #OR if two uppercase letters follow an uppercase letter or come before another uppercase letter then extract all letters in between and extract it into C2

        df2$C3=  str_extract(df1$Unit, "(?<=[:digit:])[A-E]$|Q.$")
    #if a the last a letter is A-E and is preceded by a digit then extract the letter into C3
    #OR if the last character is preceded by the letter Q then extract Q and the character

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

I think you can "encode" the conditions in a single regex wrapping the separate values with capturing groups and then use str_match to actually access those captures to later use them to create the columns:

library(stringr)
df <- data.frame(ID=c(1,2,3,4,5,6,7,8), UNIT=c("3ASD","3C14E","3D5E","3E15E","3ESK","3B14A","3BHHQ2","3E101"))
rx = "^([0-9][[:upper:]])([0-9]{1,3}|S[KD]|HH)([[:upper:]][0-9]*)?$"
match_table <- str_match(df$UNIT, rx)
df$C1 <- match_table[,2]
df$C2 <- match_table[,3]
df$C3 <- match_table[,4]
> df
  ID   UNIT C1  C2   C3
1  1   3ASD 3A  SD <NA>
2  2  3C14E 3C  14    E
3  3   3D5E 3D   5    E
4  4  3E15E 3E  15    E
5  5   3ESK 3E  SK <NA>
6  6  3B14A 3B  14    A
7  7 3BHHQ2 3B  HH   Q2
8  8  3E101 3E 101 <NA>

See the regex demo.

Details

  • ^ - start of string
  • ([0-9][[:upper:]]) - Group C1: a digit and then an uppercase letter
  • ([0-9]{1,3}|S[KD]|HH) - Group C2: 1, 2 or 3 digits, or SK, SD or HH
  • ([[:upper:]][0-9]*)? - an optional Group C3: an uppercase letter followed with 0+ digits
  • $ - end of string,

Upvotes: 2

Related Questions