duanmao1975 2012-12-10 15:30
浏览 95
已采纳

用于分解配方列表元素的RegEx语法

I am processing a list of recipe ingredients, an example of which looks like this:

Peanuts, Wheat Starch, Vegetable Oil, Modified Starch, Sugar, Mumbai Spice Flavour [Onion Powder, Herbs and Spices (Cumin, Curry Powder, Chilli Powder, Coriander), Garlic Powder, Potassium Chloride, Yeast Extract, Yeast Powder (contains Gluten and Barley), Citric Acid, Flavouring (contains Barley, Soya, Wheat, Celery)], Rice Flour, Salt, Colours (Concentrated Beetroot Juice, Curcumin, Paprika Extract).

I wish to explode each ingredient into an array (using PHP), seperated by commas. The problem I have is that some ingredients are sub-divided. In this example, the components of 'Mumbai Spice Flavour' are delimited by square brackets, and contains some ingredients, the sub-ingredients are which are then delimited by regular brackets.

A standard:

explode(",", $recipeStr) 

will give me a very messy result, so I'm looking for a Regular Expression statement that will explode each distinct element into an array, to take account of the optional square brackets, and optional sub-brackets. It also needs to be able to handle brackets that are not nested within square brackets.

The desired result would be an array list that looks like:

-Peanuts
-Wheat Starch
-Vegetable Oil
-Modified Starch
-Sugar
-Mumbai Spice Flavour [Onion Powder, Herbs and Spices (Cumin, Curry Powder, Chilli Powder, Coriander), Garlic Powder, Potassium Chloride, Yeast Extract, Yeast Powder (contains Gluten and Barley), Citric Acid, Flavouring (contains Barley, Soya, Wheat, Celery)]
-Rice Flour
-Salt
-Colours (Concentrated Beetroot Juice, Curcumin, Paprika Extract)

I am not very good at RegEx syntax, and so if any answer could also explain the syntax logic that would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dongxuan2015 2012-12-10 15:43
    关注

    This seems to work (but maybe it's not the best solution) :)

    preg_match_all('/\w[\w\s-]*(?:\[.*?\]|\(.*?\))?/', $string, $matches);
    

    It's checking word character followed by 0 or more characters/spaces/dashes (add anything you want to capture to this group), then followed either by [...] or (...) or nothing (but brackets of the same type cannot be nested

    So you can have:

    - something
    - anything [...]
    - something different (...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备