finding strings with regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm trying to use DataView to find the row number in the datatable that
contains "Rich" in it so that I can highlight it. It works fine when I enter
the entire string (i.e. Richard), but I can't seem to make a search for
"Rich" recognize that Richard is also what I want.

The problem seems to be here:

DataView dv = tCat.DefaultView;
Regex reg = new Regex(@"^Rich");
int i = dv.Find(reg);

When I have "Richard" instead of @"^Rich", it highlights the row just fine.
Any suggestion?
Thanks!!!
Mel
 
You don't need a Regular Expression for this. Use IndexOf instead. It's
quicker. If the value returned is greater than -1, the substring exists.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Does this work if I'm trying to find a string in a datatable column? I also
need to it to be case insensitive, and regex is all I found to do this so far.
The whole code I have is:

// tCat - the table name
// searchThis - the string I want to find
// dg - the datagrid
DataView dv = tCat.DefaultView;
Regex reg = new Regex(searchThis,RegexOptions.IgnoreCase);
if (searchBy == "cust")
{
dv.Sort = "Customer ASC";
int i = dv.Find(reg);
if(i > dv.Table.Rows.Count || i < 0){MessageBox.Show("No file found");}
else {dg.CurrentRowIndex = i; dg.Select(i);}
}

I can't understand why, if searchThis = "string", and regex will find the
row with "string", that it won't recognize "str" when typed as @"^str".

Thanks again for any help!!!
Mel
 
The only reason dv.Find(reg) was working for you is because that evaulates to
dv.Find(reg.ToString());
If reg was created as
Regex reg = new Regex("^Rich");
then reg.ToString() will return "^Rich"

dv.Find(string) performs an exact match on the column that was sorted by
when creating the view, so since there isn't a row where the Customer is
"^Rich", then the .Find() fails. If you need wildcards, you'll have to use
the .Select() method off of the DataTable. You also specify whether you want
the DataTable to be case sensitive, not the search. The code below will
return the first matching Richard to the search criteria of "rich".

tCat.CaseSensitive = false;
searchThis = "rich";
DataRow[] dr = tCat.Select("Customer LIKE '" + searchThis + "*'");
if ( dr.Length > 0) {
// now that we have the full customer name, we can .Find() for it
int i = dv.Find((string)dr[0]["Customer"]);
}

Note that .Find() on both DataView and DataTable performs MUCH better than
DataTable.Select() does, as it uses indexes instead of scanning the entire
table.
 
Regular Expressions work with strings. IndexOf is a method of System.String.
So, yes.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Hi Mel,

You can use the Select command of the datatable to filter the data. It will
return a collection of datacolumns.

Good Luck,
 
It works - thanks so much!
Mel

Kevin Spencer said:
Regular Expressions work with strings. IndexOf is a method of System.String.
So, yes.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
My pleasure, Mel!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Back
Top