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

finding strings with regex

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

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

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

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

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

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

P: n/a
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.