471,627 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

finding strings with regex

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
Dec 6 '05 #1
7 2705
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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

Dec 6 '05 #2
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

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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


Dec 6 '05 #3
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.

"melanieab" wrote:
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

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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


Dec 6 '05 #4
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:B9**********************************@microsof t.com...
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

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
> 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


Dec 6 '05 #5
Hi Mel,

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

Good Luck,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"melanieab" wrote:
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

Dec 7 '05 #6
It works - thanks so much!
Mel

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:B9**********************************@microsof t.com...
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

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
> 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


Dec 7 '05 #7
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
It works - thanks so much!
Mel

"Kevin Spencer" wrote:
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.

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:B9**********************************@microsof t.com...
> 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
>
> "Kevin Spencer" wrote:
>
>> 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.
>>
>> "melanieab" <me*******@discussions.microsoft.com> wrote in message
>> news:9A**********************************@microsof t.com...
>> > 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
>>
>>
>>


Dec 7 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Paul K | last post: by
6 posts views Thread by Christian Blackburn | last post: by
6 posts views Thread by Johny | last post: by
5 posts views Thread by shapper | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.