简体   繁体   中英

Is it possible to use R's base::strsplit() without consuming pattern

I have a string that consists entirely of simple repeating patterns of a [:digit:]+[AZ] for instance 12A432B4B.

I want to to use base::strsplit() to get:

[1] "12A" "432B" "4B"

I thought I could use lookahead to split by a LETTER and keep this pattern with unlist(strsplit("12A432B4B", "(?<=.)(?=[AZ])", perl = TRUE)) but as can be seen I get the split wrongly:

[1] "12"   "A432" "B4"   "B" 

Cant get my mind around a pattern that works with this strsplit strategy? Explanations would be really appreciated.

Bonus : I also failed to use back reference in gsub (eg - pattern not working `gsub("([[:digit:]]+[AZ])+", "\\1", "12A432B4B"), and can you retrieve more than \\1 to \\9 groups, say if [:digit:]+[AZ] repeats for more than 9 times?

We can use regex lookaround to split between an upper case letter and a digit

strsplit(str1, "(?<=[A-Z])(?=[0-9])", perl = TRUE)[[1]]
#[1] "12A"  "432B" "4B" 

data

str1 <- "12A432B4B"

The pattern mentioned in the post can be used as it is in str_extract_all :

str_extract_all(string, '[[:digit:]]+[A-Z]')[[1]]
#[1] "12A"  "432B" "4B"  

Or in base R:

regmatches(string, gregexpr('[[:digit:]]+[A-Z]', string))[[1]]

where string is:

string <- '12A432B4B'

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