# Regex Capturing Groups

**URL:** <https://connect.superhuman.com/t/regex-capturing-groups/44042>\
**Category:** Ask the Community\
**Created:** [October 31, 2023, 6:49pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042 "2023-10-31T18:49:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lin\_Silver](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/lin_silver/32/22236_2.png) [@Lin\_Silver](https://connect.superhuman.com/u/Lin_Silver)\
**Post date:** [October 31, 2023, 6:49pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/1 "2023-10-31T18:49:39Z")

</div>

![Screen Shot 2023-10-31 at 2.49.01 PM](https://global.discourse-cdn.com/coda1/original/3X/1/0/102956f9b1dcbf408a5d6bc61b3de472492667ea.png)

I have a table with many similar Body texts as show in this image where I’m wanting to take out specific groups from the text. Coda doesn’t seem have the ability with regex to output the match cases, so what I have done in the past is match everything except what I want and use regexreplace() and replace with " ".

For this case my body texts often have multiple matches, and I have gotten these with regex capturing groups in python. Green highlight would be the capturing groups. Coda doesn’t seem to work with capturing groups which is making this difficult for me.

Not sure of the best way to solve this! Thanks for the help!

Edit: Here are some of the regex cases I’ve tried using in the regexreplace() function. Note they may not all work as regex expressions hahaha  
._?(Apple Cider \d{4}|$)  
(?:(?!Apple Cider \d{4}).)_  
._?Apple Cider\d{4}._  
^(?:(?!Apple Cider\d{4}).)\*$

---

<div class="post-metadata">

**Author:** ![Max\_OBrien](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/max_obrien/32/49285_2.png) [@Max\_OBrien](https://connect.superhuman.com/u/Max_OBrien)\
**Post date:** [October 31, 2023, 9:24pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/2 "2023-10-31T21:24:50Z")

</div>

do you need the RegexExtract() function?

see [https://coda.io/d/CodaRegexTool\_dOwRXfmW5Iu](https://coda.io/d/CodaRegexTool_dOwRXfmW5Iu)

---

<div class="post-metadata">

**Author:** ![Christiaan\_Huizer](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/christiaan_huizer/32/28269_2.png) [@Christiaan\_Huizer](https://connect.superhuman.com/u/Christiaan_Huizer)\
**Post date:** [November 1, 2023, 11:16am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/3 "2023-11-01T11:16:59Z")

</div>

hi @Lin_Silver ,

I am not into Regex that much, it is a powerful but different syntax compared to Coda.

Below the outcome I generated with the Cod Formula Language.

 ![image](https://global.discourse-cdn.com/coda1/original/3X/7/2/72baa5bc00ac359a5f87c41b87b0370a85308ca9.png)

First we get the items that fit  
Second we add the numbers  
Third we apply a rule to get the desired outcome:

```auto
thisRow.item.Lower().Split(" ").WithName(theItems,

thisRow.body.Lower().Split(" ").Filter(
  CurrentValue.Contains(theItems) or
  CurrentValue.IsNumber()).WithName(Outcome,
    
 Sequence(1,Outcome.Count()).ForEach(
   SwitchIf(
  Outcome.Nth(CurrentValue).IsNumber().Not() and
  Outcome.Nth(CurrentValue +1).IsNumber().Not() and
  Outcome.Nth(CurrentValue + 2).IsNumber(),
    Format("{1} {2} {3}",
      Outcome.Nth(CurrentValue),
      Outcome.Nth(CurrentValue + 1),
      Outcome.Nth(CurrentValue + 2))).Filter(CurrentValue.IsNotBlank()).ToText()
 )))

```

As you can see I introduced lower to take care of capitals that maybe forgotten. This part you can remove it does not serve you.

Hope it helps, Cheers, Christiaan

---

<div class="post-metadata">

**Author:** ![Rickard\_Abraham](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/rickard_abraham/32/35007_2.png) [@Rickard\_Abraham](https://connect.superhuman.com/u/Rickard_Abraham)\
**Post date:** [November 1, 2023, 12:42pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/4 "2023-11-01T12:42:02Z")

</div>

Like Max said, there is one more (experimental) regex function, but it only returns the first match

`RegexExtract(Body, [Wanted text] + " \d{4}")` → Apple Cider 2023

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [November 1, 2023, 12:59pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/5 "2023-11-01T12:59:22Z")

</div>

@Max_OBrien is right, there is a hidden `RegexExtract` function that can return you all regex matches when you use it with the `g` flag:

 ![image](https://global.discourse-cdn.com/coda1/original/3X/0/6/065bf2734aacadd07e389f4806fc34c2641fcc95.png)

There’s no way to separately extract capturing groups though; this formula will always return the whole match (either the first one without the global flag, or a list of matches when `"g"` is specified). So unless you need to use backreferences, no need to bother about carefully setting up your capturing and non-capturing groups — you can simply just use brackets `()` for groups without the `?:` `?<name>` etc.

There are ways to do it with only the “official” RegexReplace function with some tricky lookaheads/lookbehinds, but that would be a lot of bother. I’d say just use RegexExtract — it’s been working for years already and I don’t think they are changing it any time soon.

---

<div class="post-metadata">

**Author:** ![Jean\_Pierre\_Traets](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/jean_pierre_traets/32/16275_2.png) [@Jean\_Pierre\_Traets](https://connect.superhuman.com/u/Jean_Pierre_Traets)\
**Post date:** [November 1, 2023, 6:56pm UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/6 "2023-11-01T18:56:17Z")

</div>

EDIT: _@Lin_Silver, sorry I didn’t read your question well and my below reply doesn’t really answer your question._

The most simple approach will be the Coda way @Lin_Silver

 ![image](https://global.discourse-cdn.com/coda1/original/3X/f/f/ff39fb313343ef9c726863785cd7765faa02567a.png)

As you see there are more road to Rome

---

<div class="post-metadata">

**Author:** ![Max\_OBrien](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/max_obrien/32/49285_2.png) [@Max\_OBrien](https://connect.superhuman.com/u/Max_OBrien)\
**Post date:** [November 2, 2023, 10:06am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/7 "2023-11-02T10:06:01Z")

</div>

also…

check out @Eric_Koleda’s pack

> **[Regex+ Pack, extend Coda with Regex+ - Coda](https://coda.io/packs/regex-20705)**
>
> \> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski 
> 
> The old adage aside,

---

<div class="post-metadata">

**Author:** ![Christiaan\_Huizer](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/christiaan_huizer/32/28269_2.png) [@Christiaan\_Huizer](https://connect.superhuman.com/u/Christiaan_Huizer)\
**Post date:** [November 2, 2023, 10:16am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/8 "2023-11-02T10:16:38Z")

</div>

that is a most interesting contribution, merci @Max_OBrien !

This summer we would have used AI to find and replace alike texts in large texts, that is no longer very well doable.

---

<div class="post-metadata">

**Author:** ![Max\_OBrien](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/max_obrien/32/49285_2.png) [@Max\_OBrien](https://connect.superhuman.com/u/Max_OBrien)\
**Post date:** [November 2, 2023, 10:42am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/9 "2023-11-02T10:42:21Z")

</div>

i may be old-fashioned, but i have a bad feeling about using AI to do things that can be done with a few lines of code or a formula.

for now, there is the cost aspect. but over time, that will drop towards zero.

but then there is the deterministic issue; my formula or few-lines-of-code approach can be designed and tested to prove (to my satisfaction or a client’s) that it will ALWAYS return a suitable result.

but AI LLMs are way less deterministic, especially as implemented in Coda. i cannot set the temperature to zero (so there is always randomness in the response) and i cannot even define the exact model version to be used (responses change as you move to newer model releases). in fact, i suspect that Coda even uses DIFFERENT LLM models from one instance to another.

i LOVE to use AI (especially Coda AI) for those tasks that benefit from the unique strengths of LLMs. and in the future i hope Coda will provide embedding features as well.

but some day (not too far off), general AI interfaces will overcome this, and i will be able to define the level of determinism i need, and the cost will be negligible.

---

<div class="post-metadata">

**Author:** ![Christiaan\_Huizer](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/christiaan_huizer/32/28269_2.png) [@Christiaan\_Huizer](https://connect.superhuman.com/u/Christiaan_Huizer)\
**Post date:** [November 2, 2023, 11:07am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/10 "2023-11-02T11:07:31Z")

</div>

I would not say you are old fashioned in this matter, what can be done easily with code is the way to go. however only few people master Regex, so for most users AI would be a good alternative. My proposal works as well, but is a bit more complicated than the Regex and it works only in this specific setting (two words with one number), which makes it weak.

I guess that settings like temperature (there are more) will be added over time and I hope they allow us to use our own keys one day. That having said, I am not sure that AI will become cheap, I doubt it actually.

I also believe that AI’s will become far better than today, so the prompts that fails us today, may helps us out in 3 months from now.

Over time, we may find a group of users ready to accept little mistakes in outcomes when the costs are very low, those who want reliable outcomes will keep using formulas in many cases, even when not strictly required and when the costs are high.

the pack will do the job I guess in this case!

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/system/32/24484_2.png) [@system](https://connect.superhuman.com/u/system)\
**Post date:** [January 31, 2024, 11:07am UTC](https://connect.superhuman.com/t/regex-capturing-groups/44042/11 "2024-01-31T11:07:44Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
