Ali Gajani
Ali Gajani

Reputation: 15091

How to extract using regex this text from a complex set of strings?

6 celery sticks, chopped
1 pear
300g/10.6oz strawberries
50g (1.7oz) porridge oats (uncooked)
1 tsp agave
100ml (3.4oz) Whole milk (soy/almond/rice/hemp/oat)
1 scoop of protein powder (approx. 20g/0.7oz)
½ avocado
100g/3.5oz of mushrooms, chopped
1 tsp extra virgin olive oil 

What I want to extract

celery sticks, pear, strawberries, porridge oats, agave, Whole milk, protein powder, avocado, mushrooms, extra virgin olive oil.

What I have tried

(^.*(\d|oz|\(|tsp|½|of)|(,|\().*$)

What I am getting

https://www.phpliveregex.com/p/rok

Click preg_split tab when you run it.

Ideally I should just be able to do match[0] and get the name.

Upvotes: 1

Views: 40

Answers (1)

anubhava
anubhava

Reputation: 785266

You may use this regex in preg_match or preg_match_all functions:

^[\d½]+\S*\h+(?:\([^)]*\)|tsp|.*? of\b)?\h*([a-z]+(?:\h+[a-z]+)*)

RegEx Demo

RegEx Details:

  • ^: Start
  • [\d½]+: Match 1+ digits or ½
  • \S*: Match 0 or more non-space characters
  • \h+: Match 1+ horizontal spaces
  • (?:\([^)]*\)|tsp|.*? of\b)?\h*: Match extra bit of characters
  • ([a-z]+(?:\h+[a-z]+)*): Match ingredient name

PHP Live Demo

Upvotes: 2

Related Questions