Forum Academy Marketplace Showcase Pricing Features

Regex help needed

Can someone please help me with regex expression.

I am trying to get “1.13” out of “1.13.16” and “1.2” out of 1.2.13"

I can’t use truncate as the numbers can vary in length.

Thanks for your help.

Is it always exactly 3 characters (dot pus 2 digits) at the end that you want to discard?

1 Like

No. All numbers can change in length. Only thing persistent is the number of periods.

Can there be multiple occurrences of the pattern in a string, or is each pattern - i.e. each dot-separated group of 3 numbers - a separate string? (That determines whether string start/end anchors can be used in the regex.)

1 Like

This is the pattern: “Number (1-3 digits).Number(1-3 digits). Number (1-3 digit)”

By 1-3 digit number I mean that the number can be “9” or “99” or “999”

I want this part extracted from left “Number(1-3 digit). Number(1-3 digit)”

Does this clarify? If not, please let me know.

Understood, but can multiple instances of the pattern occur within a single string? Can you provide an actual example to give some context?

Sure!

So it’s a unique category ID representing hierarchy and number.

Let’s take an example of hierarchy:

Household>> Pantry >> Chips & Snacks

Here
Household = Level 1 ( Number 2). ID=> 2
Pantry = Level 2 (Number 30). ID=> 2.30
Chips & Snacks = Level 3 (Number 5)=> ID 2.30.5

Maximum of 3 levels are possible. Meaning each category can have grandchildren ( SubSubCategory)

When the category ID with 3 levels, I want to be able to extract the ID of its parent. In the example above, when ID 2.30.5 is visible, I want to extract 2.30 out of it.

Multiple instances of the pattern would not occur within a single string.

Sorry if it still doesn’t clarify what you are asking.

Ok, so if you’re only ever dealing with a single occurrence of the pattern at a time, then the simple regex expression \d+\.\d+ should work.

Note that you should use :first item after :extract with Regex because the latter always returns a list of texts. In your case, there will only ever be one item in that list (if I understand correctly), so you could probably get by without the :first item operator (because of Bubble’s auto type conversion), but it’s good practice (for self-documenting reasons) to use it anyway.

EDIT

And if there’s a chance that white space could appear on either side of the pattern, you might want to throw in a :trimmed operator before extracting with regex.

1 Like

Thanks Steve. It solved the issue. Much Appreciated!

1 Like