Nick Res
Nick Res

Reputation: 2244

regular expressions challenge

I'm trying to grab only 1 piece of this string:

Serving Size: 1 cup, halves, Calories: 48.6, Fat: 0.5g, Carbs: 11.7g, Protein: 1g

and many others like it. Although each string will have different variables. For example the string above might look like this:

Serving Size: 100 oz, Calories: 48.6, Fat: 0.5g, Carbs: 11.7g, Protein: 1g

And so on...

So, I right now I'm trying to get the "oz" or "cup" part of the string ONLY.

The regex I've tried looks something like this:

(?<=Serving Size:\s\d*\s)

Although in rubular it keeps saying "invalid".

Although if I do

(?<=Serving Size:)\s\d*\s

It will match the number perfectly... I'm trying to exclude the number and match the measure type.

How can I do this?

Upvotes: 1

Views: 162

Answers (3)

Tallboy
Tallboy

Reputation: 13407

/Serving Size: (.+), Calories: (.+), Fat: (.+), Carbs: (.+), Protein: (.+)/

Use capture groups and then access them with $1, $2, $3, etc.

Upvotes: 1

Note: This answers why the regex is not working. Sergio Tulentsev's answer already gives a good solution for how to get the desired result.

If I interpret your intent correctly, (?<=Serving Size:\s\d*\s) is the look-behind, which I assume is followed by \w+ or something to match the name of the measurement unit. The problem with your look-behind (and the reason (?<=Serving Size:) does work) is that, unlike a look-ahead, a look-behind must have a defined length — including \d* causes it to be indefinite.

My recommendation would be to capture the units, then take the needed information from the result. Something like

/Serving Size:\s\d*\s(\w+)/

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

What about this one?

s = "Serving Size: 1 cup, halves, Calories: 48.6, Fat: 0.5g, Carbs: 11.7g, Protein: 1g"

regex = /Serving Size:\s*(?<amount>\d+)\s*(?<units>\w+)/

m = s.match(regex) # => #<MatchData "Serving Size: 1 cup" amount:"1" units:"cup">
m['units'] # => "cup"
m['amount'] # => "1"

Upvotes: 6

Related Questions