Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R regex - match until first letter-space-digit combination from the end

Tags:

regex

r

gsub

I have the following string vector:

x = c("Breakfast Sandwich 12.6 - 18.4oz 4 - 8ct", 
      "Buffalo Wing 7.6 - 10 oz", 
      "Asstd Cafe Appetizer 8 - 20", 
      "16in Extra Lrg Pepperoni 45.5oz") 

I need to move the size to the beginning of the string, but I can't create a proper regex call for it. If more than one combination is found, move only the last one. The part that is moved will always be preceded by a letter and space. The desired output will be:

"4 - 8ct Breakfast Sandwich 12.6 - 18.4oz", 
"7.6 - 10 oz Buffalo Wing", 
"8 - 20 Asstd Cafe Appetizer", 
"45.5oz 16in Extra Lrg Pepperoni"

I think, non-greedy matching until something like [a-z] [0-9].*? is found? Or may be use split instead? Could you, please, help me with it? Thank you in advance!

B.t.w., if there is no one step solution for all test cases, a series of separate gsub will work as well.

like image 487
Alexey Ferapontov Avatar asked Feb 08 '23 18:02

Alexey Ferapontov


2 Answers

This seems to handle the cases you mentioned:

sub("(.*[a-z]{1}) ([0-9.]+\\s*-?\\s*[0-9.]*\\s*[a-z]*\\s*)$", "\\2 \\1", x)

like image 116
Max Avatar answered Feb 11 '23 06:02

Max


Try this:

 gsub('(.*(?<=\\w)) (\\d.*$)','\\2 \\1',x,perl=T)
[1] "4 - 8ct Breakfast Sandwich 12.6 - 18.4oz" "7.6 - 10 oz Buffalo Wing"                 "8 - 20 Asstd Cafe Appetizer"             
[4] "45.5oz 16in Extra Lrg Pepperoni"  
like image 37
Shenglin Chen Avatar answered Feb 11 '23 08:02

Shenglin Chen