# Doesn't capturing groups within non capturing groups work in regex?

**URL:** <https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029>\
**Category:** General Usage\
**Tags:** regex\
**Created:** [April 28, 2023, 8:55am UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029 "2023-04-28T08:55:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [April 28, 2023, 8:55am UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/1 "2023-04-28T08:55:32Z")

</div>

I’m looking to capture a pattern like `"=2.4.7.9_"` and be able to extract the numbers.

I try to match it with `r"=(\d+)(:\.(\d+))*_"` but that doesn’t seem to work. I expect each number to end up in a capture group but instead it just doesn’t match at all. Why is this? What should I write instead?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 28, 2023, 11:09am UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/2 "2023-04-28T11:09:29Z")

</div>

That’s incorrect syntax for non-capturing groups. It should be `=(\d+)(?:\.(\d+))*_` instead (note the `?`).

---

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [May 2, 2023, 9:33am UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/3 "2023-05-02T09:33:04Z")

</div>

For some reason I was sure I knew the syntax, but evidently not. I even looked up the syntax at [pcre.org](http://pcre.org), but I still couldn’t see what I had done wrong.

Thanks for spotting it.

---

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [May 10, 2023, 12:17pm UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/4 "2023-05-10T12:17:00Z")

</div>

For people who might stumble across this topic in the future, I would like to add that using `r"=(\d+)(?:\.(\d+))*_"` the way I intended doesn’t work, anyway.

The number of capture groups in the match object is statically allocated from the number of capturing parentheses encountered in a lexical analysis of the regular expression. That means, that when repeating the non-capturing group in the above regular expression, the second capturing group of the match object would be replaced.

I wasn’t aware that you can label the capturing groups and use a dictionary-like interface to the match objects, but had I been, this behaviour would have been self evident since keys have to be unique. I ended up capturing the whole collection of numbers and dots in one fell swoop and used split instead.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 10, 2023, 12:34pm UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/5 "2023-05-10T12:34:24Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7be5ff89dd730b02c356821aa265a996f46ef44c.png)

I usually find it super useful to check regexps against e.g [regex101.com](http://regex101.com) (see above) or [regexpal.com](http://regexpal.com).

---

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [May 13, 2023, 2:53pm UTC](https://discourse.julialang.org/t/doesnt-capturing-groups-within-non-capturing-groups-work-in-regex/98029/6 "2023-05-13T14:53:26Z")

</div>

Yup. Added them to my bookmarks. Ta!
