Alexey Ferapontov
Alexey Ferapontov

Reputation: 5169

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

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.

Upvotes: 4

Views: 187

Answers (3)

Shenglin Chen
Shenglin Chen

Reputation: 4554

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"  

Upvotes: 3

David Arenburg
David Arenburg

Reputation: 92292

This seem to work for your cases too

sub("(.*[a-zA-Z]) +(\\d.*)", "\\2 \\1", x)
# [1] "4 - 8ct Breakfast Sandwich 12.6 - 18.4oz" "7.6 - 10 oz Buffalo Wing"                
# [3] "8 - 20 Asstd Cafe Appetizer"              "45.5oz 16in Extra Lrg Pepperoni" 

Upvotes: 2

Max
Max

Reputation: 544

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)

Upvotes: 3

Related Questions