lets @ exemple:
abc_def_ghi_jkl if choose n = 1, want output be:
group1 = abc_def_ghi group2 = jkl if choose n = 2 want output be:
group1 = abc_def group2 = ghi_jkl note _ seperated 2 groups removed.
for figured out how select last group select _:
(?:.(?!(?=\_)))+$ note2 focusing on regex part code used in r if helps solution.
a possibility split on nth occurrence of _ end of string:
strsplit("abc_def_ghi_jkl", "_(?=([^_]*_){0}[^_]*$)", perl = t) # ^ # can modify quantifier here #[[1]] #[1] "abc_def_ghi" "jkl" # split on 1st strsplit("abc_def_ghi_jkl", "_(?=([^_]*_){1}[^_]*$)", perl = t) #[[1]] #[1] "abc_def" "ghi_jkl" # split on 2nd strsplit("abc_def_ghi_jkl", "_(?=([^_]*_){2}[^_]*$)", perl = t) #[[1]] #[1] "abc" "def_ghi_jkl" # split on 3rd _(?=([^_]*_){2}[^_]*$) looks _ before pattern ([^_]*_){2}[^_]*$ via ?= ahead syntax , pattern starts end of string $ , skips non _ patterns [^_]* , matches ([^_]*_) number of occurrences , after split on specified _.
update str_match stringr package:
str_match("abc_def_ghi_jkl", "(.*)_((?:[^_]*_){0}[^_]*$)")[,2:3] # [1] "abc_def_ghi" "jkl" str_match("abc_def_ghi_jkl", "(.*)_((?:[^_]*_){1}[^_]*$)")[,2:3] # [1] "abc_def" "ghi_jkl" str_match("abc_def_ghi_jkl", "(.*)_((?:[^_]*_){2}[^_]*$)")[,2:3] # [1] "abc" "def_ghi_jkl"
Comments
Post a Comment