[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular Expression newbie question about upper & lower case

Peter Vanderhaden

9/30/2007 4:49:00 PM

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....
--
Posted via http://www.ruby-....

5 Answers

SpringFlowers AutumnMoon

9/30/2007 4:52:00 PM

0

Peter Vanderhaden 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

--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/30/2007 4:57:00 PM

0

Peter Vanderhaden 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
--
NP: Die Apokalyptischen Reiter - Human End
Jabber: sepp2k@jabber.org
ICQ: 205544826

Peter Vanderhaden

9/30/2007 5:01:00 PM

0

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

SpringFlowers AutumnMoon wrote:
> Peter Vanderhaden 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

--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/30/2007 5:08:00 PM

0

Peter Vanderhaden wrote:
> Does the i at the end of the expression indicate
> case-insensitivity?

Yes.


--
NP: Anathema - Pulled under at 2000 metres a second
Jabber: sepp2k@jabber.org
ICQ: 205544826

SpringFlowers AutumnMoon

9/30/2007 5:10:00 PM

0

Peter Vanderhaden 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.

--
Posted via http://www.ruby-....