Regular Expression newbie question about upper & lower case

P

Peter Vanderhaden

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....
 
S

SpringFlowers AutumnMoon

Peter said:
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
 
S

Sebastian Hungerecker

Peter said:
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
 
P

Peter Vanderhaden

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

SpringFlowers said:
Peter said:
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
 
S

SpringFlowers AutumnMoon

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top