# Regular Expression newbie question about upper & lower case

**URL:** <https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941>\
**Category:** Ruby\
**Created:** [September 30, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941 "2007-09-30T18:48:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/1 "2007-09-30T18:48:32Z")

</div>

I want to check for both upper & lower case answers when a user keys in  
a response to a question, but I haven’t been able to find an example of  
the syntax. What I’m looking for is something like this:

```
If $ans = [Yy]*

```

I know this syntax isn’t correct, but what I want it to do is accept Y,  
y, yes, YeS, yippee, etc. Would someone mind posting an example?  
Thanks…

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/2 "2007-09-30T18:51:48Z")

</div>

Peter V. wrote:

> I want to check for both upper & lower case answers when a user keys in  
> a response to a question, but I haven’t been able to find an example of  
> the syntax. What I’m looking for is something like this:
> 
> ```
> If $ans = [Yy]*
> 
> ```
> 
> I know this syntax isn’t correct, but what I want it to do is accept Y,  
> y, yes, YeS, yippee, etc. Would someone mind posting an example?  
> Thanks…

something like

a =~ /^y/i

will do

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/3 "2007-09-30T18:57:37Z")

</div>

Peter V. wrote:

> I want to check for both upper & lower case answers when a user keys in  
> a response to a question, but I haven’t been able to find an example of  
> the syntax. What I’m looking for is something like this:
> 
> ```
> If $ans = [Yy]*
> 
> ```
> 
> I know this syntax isn’t correct, but what I want it to do is accept Y,  
> y, yes, YeS, yippee, etc. Would someone mind posting an example?  
> Thanks…

If you only care whether the first character is a y you can just do  
if ans =~ /^y/i #(match beginning of the line followed by a y,  
# ignoring case)  
or  
if ans[0,1].lowercase == “y”

If you just want y, yes, yippee, yeah and yo, but you don’t want “you  
suck” or  
yodeling, you could use this:  
if ans =~ /^y$|yes|yippee|yeah|yo/i

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/4 "2007-09-30T19:08:17Z")

</div>

Peter V. wrote:

> Does the i at the end of the expression indicate  
> case-insensitivity?

Yes.

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/5 "2007-09-30T19:10:06Z")

</div>

Peter V. wrote:

> > a =~ /^y/i
> 
> Thank you much. Does the i at the end of the expression indicate  
> case-insensitivity?

yes, it is a modifier, and i for case insensitivity and m for multiline  
for a dot “.” to match everything including a “\n” character.

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [September 30, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/regular-expression-newbie-question-about-upper-lower-case/115941/6 "2007-09-30T19:01:10Z")

</div>

Thank you much. Does the i at the end of the expression indicate  
case-insensitivity?

SpringFlowers AutumnMoon wrote:

> Peter V. wrote:
> 
> > I want to check for both upper & lower case answers when a user keys in  
> > a response to a question, but I haven’t been able to find an example of  
> > the syntax. What I’m looking for is something like this:
> > 
> > ```
> > If $ans = [Yy]*
> > 
> > ```
> > 
> > I know this syntax isn’t correct, but what I want it to do is accept Y,  
> > y, yes, YeS, yippee, etc. Would someone mind posting an example?  
> > Thanks…
> 
> something like
> 
> a =~ /^y/i
> 
> will do
