# Problems with regular expressions and parenthesis

**URL:** <https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311>\
**Category:** Ruby\
**Created:** [November 1, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311 "2008-11-01T21:33:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [November 1, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311/1 "2008-11-01T21:33:39Z")

</div>

hi,  
i’m having some trouble with regular expressions in ruby.  
If I have a regular expression like this:  
^([\x20-\x7E]|\w){1,40}$, it will crash ruby if there are more than  
forty characters in it. I don’t understand what is wrong with this  
expression, if my expression is changed to  
^([\A-Za-z]|\w){1,40}$ it works perfectly well  
so does this one: ^([\x20-\x7E]|\w){1,40}$  
am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [November 2, 2008, 12:57am UTC](https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311/2 "2008-11-02T00:57:53Z")

</div>

Daniel F. wrote:

> hi,  
> i’m having some trouble with regular expressions in ruby.  
> If I have a regular expression like this:  
> ^([\x20-\x7E]|\w){1,40}$, it will crash ruby if there are more than  
> forty characters in it. I don’t understand what is wrong with this  
> expression, if my expression is changed to  
> ^([\A-Za-z]|\w){1,40}$ it works perfectly well  
> so does this one: ^([\x20-\x7E]|\w){1,40}$

What do you mean by “so does this one”?

It’s the same as the first one.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [November 2, 2008, 2:21am UTC](https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311/3 "2008-11-02T02:21:45Z")

</div>

Daniel F. wrote:

> hi,  
> i’m having some trouble with regular expressions in ruby.  
> If I have a regular expression like this:  
> ^([\x20-\x7E]|\w){1,40}$, it will crash ruby if there are more than  
> forty characters in it. I don’t understand what is wrong with this  
> expression, if my expression is changed to  
> ^([\A-Za-z]|\w){1,40}$ it works perfectly well  
> so does this one: ^([\x20-\x7E]|\w){1,40}$  
> am I missing something obvious?

Sorry, copied it wrong:  
/^([A-Za-z]|\w|\s){1,40}$/  
Could it have something to do with some parts of it overlapping?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [November 2, 2008, 2:27am UTC](https://www.ruby-forum.com/t/problems-with-regular-expressions-and-parenthesis/152311/4 "2008-11-02T02:27:58Z")

</div>

Daniel F. wrote:

> Daniel F. wrote:
> 
> > hi,  
> > i’m having some trouble with regular expressions in ruby.  
> > If I have a regular expression like this:  
> > ^([\x20-\x7E]|\w){1,40}$, it will crash ruby if there are more than  
> > forty characters in it. I don’t understand what is wrong with this  
> > expression, if my expression is changed to  
> > ^([\A-Za-z]|\w){1,40}$ it works perfectly well  
> > so does this one: ^([\x20-\x7E]|\w){1,40}$  
> > am I missing something obvious?
> 
> Sorry, copied it wrong:  
> /^([A-Za-z]|\w|\s){1,40}$/  
> Could it have something to do with some parts of it overlapping?

Sorry, got it wrong again, these expression do not work:  
/^([A-Za-z]|\w|\s){1,40}$/  
^([\x20-\x7E]|\w){1,40}$

and these do:  
/^([\x20-\x7E]|\s){1,40}$/  
/^([A-Za-z]|\w){1,40}$/  
/^([A-Za-z]|\w){1,40}$/  
sorry about that
