Multiply within the :find&replace using regex

Hi,

I want to build an app that provides users with recipes to cook. Those recipes shall adapt dynamically to the number of people provided by the users. For example the basic recipe in the database will say “Take 1 egg(s)”. When the number of people is 5 I want the recipe to adapt automatically: “Take 5 egg(s)”.

So far I tried to save the text as “Take #1# egg(s)” and then to use the :find&replace with a regex expression.

image

I however am not able to multiply the number between the hashtags with the dynamic number of people. I can’t think of any expression for the “Replace by” field in order to get this result.

Thanks a lot in advance!

I would think saving any recipe with a list for ingredient and amount along with how many people that recipe should serve as an input. When I save the recipe I’d convert everything to the equivalent of 1 serving.

Then when I recall and display the amounts

I’d have an input for how many servings I want to cook

And multiply the saved amount by that input.

Hi @jared.gibb

thanks for your reply. I’m also saving a list of ingredients, but I want additionally that the numbers in description to be dynamic…

You won’t be able to do it inside the regex, as “*” and “#” probably means something arcane.

You should be able to do this as a regular F&R.

But I think what you are trying to do is …

Find the number between the two #x# marks. Multiply x by y. Replace #x# with z.

Is that right?

Take #1# egg(s) > 1 * 5 = 5 > Take 5 egg(s).

I think you may well have to have a regex within a regex for this.

Hi @NigelG

that is correct and thanks a lot for this idea. I just tried it and it is working. I used the :find&replace to find the expression and replace it by the multiple of the relevant state and the regex extraction of the text.

The problem however is that in a block of text I can have different relevant numbers (“take #1# egg and #250# ml of milk”) . The :find&replace doesn’t allow me to reference each pattern found dynamically. I could have a different sign for each number #1#, {2}, [3] etc. and have several :find&replace for that, but it would always be limited to certain amount of numbers…

Yes, I think that is why you want the F&R via regex to pull back what is in the #2#

And then do the maths in the subsequent F&R rather than all at once.

I’m not sure, if I get it. In the second F&R I’m not able to reference the number between the signs in the “Replace by” area. I therefor don’t understand how I could do the math there?

What I’m doing right now is this:

image

image

Bubble doesn’t want me to leave the regex extraction as a list and have every item be multiplied. That’s why I have the first item selected.

Do a Find and Replace With Regex on the #x# using this Regex

#.*?#

Then Use your Regex to extract the number, turn it into an actual number and sum (because regex can return a list) and multiply.

Video up on #NoCodeBoss

Thanks a lot for the video! As mentioned the problem remains for a text that contains several different numbers. But I’ll just take the workaround mentioned above.

1 Like

Take #2# eggs and #3# cups of flour is taking it up a notch :slight_smile:

:grin: Yeah, true. Thanks a lot again!

This topic was automatically closed after 70 days. New replies are no longer allowed.