Regular Expression Question

  • Thread starter Thread starter Amy L.
  • Start date Start date
A

Amy L.

I was looking at some Regular Expressions and I noticed some of them have
the following character in them "/i". For example:
\b(?:college|university)\s+diplomas/i

In the .Net documents I can't seem to find any reference to /i - can someone
tell me exactly what /i does and if it is supported in the .Net regular
expression parser?

Amy
 
Amy L. said:
I was looking at some Regular Expressions and I noticed some of them have
the following character in them "/i". For example:
\b(?:college|university)\s+diplomas/i

In the .Net documents I can't seem to find any reference to /i - can someone
tell me exactly what /i does and if it is supported in the .Net regular
expression parser?

Case Insensitive. RegexOptions.IgnoreCase.
 
I was looking at some Regular Expressions and I noticed some of
them have the following character in them "/i". For example:
\b(?:college|university)\s+diplomas/i

In the .Net documents I can't seem to find any reference to /i -
can someone tell me exactly what /i does and if it is supported
in the .Net regular expression parser?

Amy,

In Perl, the /i at the end of a regular expression tells the regex
parser to perform case-insensitive matches.

Case-insensitive matching is supported two ways in .Net, but the
syntax is different.

The first way is to use the RegexOptions enumeration:

bool isMatch = Regex.IsMatch("Hello, world!", "WORLD",
RegexOptions.IgnoreCase);

The second way is to use the (?i) construct w/i the regex:

bool isMatch = Regex.IsMatch("Hello, world!", "(?i)WORLD");

Note that the (?i) has to be the first thing in the regex.
A regex like "WORLD(?i)" won't work.

See the MSDN documentation for more info:

http://msdn.microsoft.com/library/d...pgenref/html/cpconmiscellaneousconstructs.asp

or

http://tinyurl.com/5lons
 
Amy L. said:
In the .Net documents I can't seem to find any reference to /i - can someone
tell me exactly what /i does and if it is supported in the .Net regular
expression parser?

/pattern/i

In Perl and other regexp implementations the i option indicates
that pattern matching should be done case-Insenstively (i.e.,
'a' appearing in pattern will match 'a' or 'A' in the input text).

The options you can specify with characters after the pattern
match expression on other platforms can be specified through
the RegexOptions in .NET. For example, when constructing
your Regex object you can specify case-insensitive matching
like this,

Regex re = new Regex( "pattern", RegexOptions.IgnoreCase);


Derek Harmon
 
Chris,

When you mention the (?i) you do not need the "/i" at the end? Right?

Also, is it more efficient to use the regex option or is the
performance the same as (?i).

It seems from the reading that you can turn the (?i) on anywhere in the
pattern where you would like case insensitivity?

Thanks,
Amy
 
(e-mail address removed) wrote in
Chris,

When you mention the (?i) you do not need the "/i" at the end?
Right?
Correct.

Also, is it more efficient to use the regex option or is the
performance the same as (?i).

I've never noticed any difference, nor am I aware of any documented
performance differences between the two.
It seems from the reading that you can turn the (?i) on anywhere
in the pattern where you would like case insensitivity?

Yes. Instead of using (?i) for the entire regex, you can use (?i:)
on individual groups:

bool isMatch = Regex.IsMatch("Hello, world!", "Hello, (?i:WORLD)");
 
Back
Top