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.
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)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With