Regex: different options on different sections of the Regex?

E

Ethan Strauss

Hi,
I would like to create a regular expression which matches
"Y" or "male". The hard part is that the "Y" must be capital, but the "male"
should be case insensitive. I think I have seen a way to have Regex options
apply differently throughout the expression, but I can't find it now. Does
anyone know if this is possible or how to do it?
Thanks!
Ethan

Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
(e-mail address removed)
 
M

Martin Honnen

Ethan said:
I would like to create a regular expression which matches
"Y" or "male". The hard part is that the "Y" must be capital, but the "male"
should be case insensitive. I think I have seen a way to have Regex options
apply differently throughout the expression, but I can't find it now. Does
anyone know if this is possible or how to do it?

I don't know of a way to apply options to different sections but with
the few characters you could spell it out e.g.
Y|[Mm][Aa][Ll][Ee]
 
E

Ethan Strauss

I was hoping to be able to get fancy, but your solution will work at least in
this case.
Thanks,
Ethan

Martin Honnen said:
Ethan said:
I would like to create a regular expression which matches
"Y" or "male". The hard part is that the "Y" must be capital, but the "male"
should be case insensitive. I think I have seen a way to have Regex options
apply differently throughout the expression, but I can't find it now. Does
anyone know if this is possible or how to do it?

I don't know of a way to apply options to different sections but with
the few characters you could spell it out e.g.
Y|[Mm][Aa][Ll][Ee]
 
M

Michael Bray

=?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=
I was hoping to be able to get fancy, but your solution will work at
least in this case.

You can dynamically change options within the .NET Regular expressions...
This does what you want:

Y|(?i-:male)

You should check out Expresso - it is a fantastic utility for building and
testing regular expressions.

Expresso: http://www.ultrapico.com/Expresso.htm

-mdb
 
M

Martin Honnen

Ethan said:
I was hoping to be able to get fancy, but your solution will work at least in
this case.

I have checked the documentation now and the .NET framework's regular
expression language indeed allows turning on options with e.g. (?i)
so this code

foreach (string s in new string[] { "Y", "y", "Male", "mAle" })
{
Console.WriteLine(Regex.IsMatch(s, "Y|(?i)male"));
}

outputs

True
False
True
True
 

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

Top