# What strings does this regex matches ? (a|b)\*

**URL:** <https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472>\
**Category:** web dev\
**Created:** [December 14, 2022, 2:23am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472 "2022-12-14T02:23:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 14, 2022, 2:23am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/1 "2022-12-14T02:23:47Z")

</div>

regepxal is showing infinite matches and shows “baaaaaaa” as a match. Doesn’t (a|b) means a or b?

- means match 0 or more times. So, it should be matching aa,aaa,bb,bbb, , etc?  
I’m not getting how baaaa is a match. Can anyone clarify please?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [December 14, 2022, 2:49am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/2 "2022-12-14T02:49:02Z")

</div>

The parentheses group a subexpression, so it’ll match any combo of `a` and `b`. It might help to use longer examples, like `"catdogdogcatcatcatcat".match(/(cat|dog)*/)`.

Regular expressions as implemented in most programming languages are a bit more complicated than regular languages from linguistics… so there are a bunch more operators and such that can muddy someone’s understanding of what exactly constitutes a match.

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 14, 2022, 3:27am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/3 "2022-12-14T03:27:31Z")

</div>

I feel I’m not getting it. I feel I’m missing something that you have good grasp of. You choose a or b and repeat, so aaaa, bbbb should be only matches. Could you help me out?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [December 14, 2022, 3:56am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/4 "2022-12-14T03:56:43Z")

</div>

> [@polaryeti](#):
>
> You choose a or b and repeat, so aaaa, bbbb should be only matches

You repeatedly choose between `a` and `b`. That’s different than choosing once and repeatedly judging the choice against the input.

You might think of it like a _precedence_ problem in the syntax. For your aaaa, bbbb example, the _choice_ happens first (a or b) and the _repetition_ happens second (zero-or-more times). But it’s the opposite, the repetition happens first and the choice happens on each repetition.

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 14, 2022, 4:40am UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/5 "2022-12-14T04:40:12Z")

</div>

Not getting it still. I guess we’ve FSM for it. Where can I read about those?

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 14, 2022, 12:11pm UTC](https://forum.kirupa.com/t/what-strings-does-this-regex-matches-a-b/655472/6 "2022-12-14T12:11:45Z")

</div>

Thank you. The perfect way to think about this for me is this:

(X)\*= X X X X X X

Here X=a or b

So,

a or b then a or b then a or b then…
