regex 如何使用正则表达式或字符串操作拆分一个复杂的字符串?

f8rj6qna  于 2023-02-25  发布在  其他
关注(0)|答案(2)|浏览(132)

我有一份配料表如下:

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"

我想把它们分成变量ingredients下的 Dataframe 中的值。
但是我在写代码时遇到了麻烦,因为分隔符|在列表中的使用方式不同,所以我想在|上拆分,它不包含在括号()或方括号[]中,但我真的不知道如何处理。
也就是说,我们最终会得到一个成分值Starch (Corn | Potato | Wheat),另一个成分值Vegetables (27%) [Pea (23%) (Flakes | Pieces) | Carrot Pieces | Onion Powder | Spinach Powder],还有一个成分值Salt(加上其他成分,但前两个对我来说更棘手)。

zbsbpyhn

zbsbpyhn1#

正则表达式修改自this answer
其思想是替换括号中的|字符(()[])到其他对象(在我的示例中为@)。剩下的|字符应该是字符串的真实的分隔符。然后使用strsplit|上拆分,并将@符号替换回|。最后,trims()以删除每个字符串末尾不需要白色。

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"
8ulbf1ek

8ulbf1ek2#

你可以使用递归正则表达式:

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"

相关问题