By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,468 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,468 IT Pros & Developers. It's quick & easy.

Regex: different options on different sections of the Regex?

P: n/a
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
et***********@promega.com
Jul 7 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ethan Strauss wrote:
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]
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 7 '08 #2

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

"Martin Honnen" wrote:
Ethan Strauss wrote:
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]
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 11 '08 #3

P: n/a
=?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=
<Et**********@discussions.microsoft.comwrote in
news:0A**********************************@microsof t.com:
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
Jul 11 '08 #4

P: n/a
Ethan Strauss wrote:
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
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.