Regex to find a string

  • Thread starter Thread starter timor.super
  • Start date Start date
T

timor.super

I have a string, for example :

string str = "abCdEfGhiJKl";

to find "def", ignoring the case, i'm doing

if (str.toLower().Contains("def"))
{
// do something
}


How to do this with regex ?

Thanks for your help
 
I've founded :

Regex regex = new Regex("[dD][eE][fF]");
if (regex.IsMatch(str))


but is it the best way ?
 
(e-mail address removed) pretended :
I've founded :

Regex regex = new Regex("[dD][eE][fF]");
if (regex.IsMatch(str))


but is it the best way ?

There is also an overload that accepts RegexOptions. Use
RegexOptions.IgnoreCase.

And there is a static variant:
if (Regex.IsMatch(str, "def", RegexOptions.IgnoreCase)) ...

Hans Kesting
 
I've founded :

Regex regex = new Regex("[dD][eE][fF]");
if (regex.IsMatch(str))

but is it the best way ?

I have a string, for example :
string str = "abCdEfGhiJKl";
to find "def", ignoring the case, i'm doing
if (str.toLower().Contains("def"))
{
// do something

How to do this with regex ?
Thanks for your help

You could set the "Options"-property of your Regex to IgnoreCase if
you don't want to check for lower and upper case of every character.

hth,
Kevin Wienhold
 

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

Back
Top