Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a complex string using regex or string manipulation?

I have a list of ingredients as follows:

Ingredients <- "Starch (Corn | Potato | Wheat) | Vegetables (27%) [Pea (23%) (Flakes | Pieces) | Carrot Pieces | Onion Powder | Spinach Powder] | Croutons (10%) (Wheat Flour | Vegetable Oil | Salt | Yeast) | Maltodextrin | Natural Flavours (Contain Milk and Soybeans) | Creamer [Contains Milk | Mineral Salts (339 or 340 | 450 or 451)] | Salt | Mineral Salt (Potassium Chloride) | Sugar | Flavour Enhancer (621) | Vegetable Oil | Bacon Powder (0.5%) | Parsley | Natural Colour (Turmeric) | Burnt Sugar | Food Acid (Lactic) | Pepper Extract"

I want to separate them into values in a dataframe under variable ingredients.

But I am having trouble writing the code because the separator | is used in various ways in the list. So I want to split on | where it's not contained within brackets () or square brackets []. And don't really know how to approach this.

That is, we'd end up with an ingredient value of Starch (Corn | Potato | Wheat) and another of Vegetables (27%) [Pea (23%) (Flakes | Pieces) | Carrot Pieces | Onion Powder | Spinach Powder] and another which is just Salt (plus other ingredients, but those first two are the trickier cases to me).

like image 552
Jay Bee Avatar asked Aug 24 '26 16:08

Jay Bee


2 Answers

Regex modified from this answer.

The idea is to substitute the | character in between parentheses (() and []) to something else (@ in my example) first. The remaining | character should be the real separator of your strings. Then use strsplit to split on | and substitute the @ symbol back to |. Finally, trims() to remove unwanted white spaces at the ends of each string.

library(dplyr)

strsplit(gsub("\\|(?=[^()]*\\))", "@", Ingredients, perl=TRUE) %>% 
           gsub("\\|(?=[^\\[\\]]*\\])", "@", ., perl=TRUE), "\\|") %>% 
  unlist() %>% 
  gsub("@", "\\|", .) %>% 
  trimws()

 [1] "Starch (Corn | Potato | Wheat)"                                                                
 [2] "Vegetables (27%) [Pea (23%) (Flakes | Pieces) | Carrot Pieces | Onion Powder | Spinach Powder]"
 [3] "Croutons (10%) (Wheat Flour | Vegetable Oil | Salt | Yeast)"                                   
 [4] "Maltodextrin"                                                                                  
 [5] "Natural Flavours (Contain Milk and Soybeans)"                                                  
 [6] "Creamer [Contains Milk | Mineral Salts (339 or 340 | 450 or 451)]"                             
 [7] "Salt"                                                                                          
 [8] "Mineral Salt (Potassium Chloride)"                                                             
 [9] "Sugar"                                                                                         
[10] "Flavour Enhancer (621)"                                                                        
[11] "Vegetable Oil"                                                                                 
[12] "Bacon Powder (0.5%)"                                                                           
[13] "Parsley"                                                                                       
[14] "Natural Colour (Turmeric)"                                                                     
[15] "Burnt Sugar"                                                                                   
[16] "Food Acid (Lactic)"                                                                            
[17] "Pepper Extract" 
like image 189
benson23 Avatar answered Aug 27 '26 07:08

benson23


You could use a recursive regex:

pat <- r"(([^\[\]|]*[\[(](?:[^\[)(\]]*(?1)?)+[\])])| ([^|]+))"
regmatches(Ingredients, gregexpr(pat, Ingredients, perl = TRUE))

[[1]]
 [1] "Starch (Corn | Potato | Wheat)"                                                                 
 [2] " Vegetables (27%) [Pea (23%) (Flakes | Pieces) | Carrot Pieces | Onion Powder | Spinach Powder]"
 [3] " Croutons (10%) (Wheat Flour | Vegetable Oil | Salt | Yeast)"                                   
 [4] " Maltodextrin "                                                                                 
 [5] " Natural Flavours (Contain Milk and Soybeans)"                                                  
 [6] " Creamer [Contains Milk | Mineral Salts (339 or 340 | 450 or 451)]"                             
 [7] " Salt "                                                                                         
 [8] " Mineral Salt (Potassium Chloride)"                                                             
 [9] " Sugar "                                                                                        
[10] " Flavour Enhancer (621)"                                                                        
[11] " Vegetable Oil "                                                                                
[12] " Bacon Powder (0.5%)"                                                                           
[13] " Parsley "                                                                                      
[14] " Natural Colour (Turmeric)"                                                                     
[15] " Burnt Sugar "                                                                                  
[16] " Food Acid (Lactic)"                                                                            
[17] " Pepper Extract"        
like image 37
KU99 Avatar answered Aug 27 '26 08:08

KU99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!