Regex Captured Groups

There doesn’t seem to be any support for captured groups. If that is the case I think this is a big oversight, since that is a very powerful feature. This question was already asked (by someone else) some time ago and no one gave any comment, so I’m asking again.

It also doesn’t seem like splitting by regex is supported either.

1 Like

Hey @JamesW,

You mean something like this?

Which displays a repeating group like this:



Looking to accelerate your app development?

Let me turn
:thinking: :tired_face: :confounded:

into
:grinning: :sunglasses: :woman_student:

Development through Coaching at https://uniqueideas.com or schedule a free intro session :gift:

Ken Truesdale
LinkedIn

No. What you did doesn’t require groups. Try “[A-Z]\w+” and you’ll get the same result. If the group was captured you could extract only the “A-Z” part (the first letter).

You can use capture groups in the Find&replace action with the regex checkbox checked.

I don’t think you understand the purpose of a capturing group. Yes, I can use groups in many places, but I can’t do anything in the expressions with the data in groups. Find and replace seems to work this way:

Text: https://www.youtube.com/watch?v=tgbNymZ7vqY&feature=youtu.be&t=83
Regex: v=(\w+)
Replace with: -
Result: [https://www.youtube.com/watch?-&feature=youtu.be&t=83]

It does the same thing, just does a full match. The group serves no purposes here. Besides, I don’t want to do a find and replace.

If groups are supported properly, then v=(\w+) would be an easy and simple way to extra stuff without the other parts of the matched regex getting included, and for find and replace, only the matched group would be replaced.

For the record, I did solve my issue by 2-3 “find and replace” operations to clean out the stuff I don’t want, but that is just silly.

1 Like

I do know exactly how capture groups work and use them in my app. Here is an example where I’m inserting an ID tag so I can control the look of emails that are pulled into my app.

You use a backreference to keep the data from the capture group(s), not replace it.

See this for further understanding: Reinserting Text Matched By Capturing Groups in The Replacement Text

If your regular expression has named or numbered capturing groups, then you can reinsert the text matched by any of those capturing groups in the replacement text. Your replacement text can reference as many groups as you like, and can even reference the same group more than once. This makes it possible to rearrange the text matched by a regular expression in many different ways.

8 Likes

I see now what you meant. You mean to match the whole string using the the regex and replace the whole content with the text extracted using a back reference. This is probably a sufficient work-around for most cases, but it doesn’t allow for an array of data matches (you just get one string). It is still only a kluge in my opinion, and an array of matched group data should be included in the expression list.

It’s not a workaround, it’s how regex works. The whole point of regex is to define a search pattern.

What I showed is not just matching one string, it’s all matching strings. It’s searching for all matches, then replacing it with what I specified in the replace section.

How replacement strings work can vary by library and I’m not entirely sure what Bubble is using (possibly PCRE2).

There are a few different ways to perform replacements as discussed here https://www.regular-expressions.info/replacetutorial.html

You didn’t get what I meant. I know it’s how regex works. :wink: What I mean is there is a lot more to regex than replacing a string to get another string. Regex has a list of matched groups in an array/list, and this should be exposed on the editor as well (for the expressions). What you showed me is a “replace sub-strings with another string” operation, and it only applies to one string to create another string. It doesn’t produce an array/list of results as the final type. I’m on StackOverflow answering lots of regex questions for many years, so I know enough about regex to know what I mean. :wink:

Hmm, I wonder if you could use @mishav’s toolbox plugin to capture a list from regex?

I already installed it for other things. I’m only bringing this up so there’s more push to get that feature in there. I wanted to make sure first that I wasn’t missing anything.

2 Likes

Hey James,

It’s an honor to meet a regex Jedi. I hear you. I think you’d have some super suggestions for improvement to the regex functionality here on Bubble. This platform is relatively new and the founders are constantly upgrading functionality. You are obviously seeking to juice Regex for more than most people are aware is possible here. Better yet, create a userful plugin for the community and sell it on the plug in store (for free or clams)

Peace my friend.

Ashley

Thank you so much for this solution :slight_smile:

Indeed, this would be useful in many situations. I often want to “extract” substrings of a larger string, and being able to access those substrings in a list would be really nice. Of course, it’s not likely to get implemented if it’s not submitted as a bug report or in an email to support.

5 Likes