473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2819
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
7
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
6
by: Christian Blackburn | last post by:
Hi Gang, When encoding HTML strings it'll convert things like " --> &rsquo and the like using Server.HTMLEncode(). However, is there a command to make sure strings don't contain valid SQL...
6
by: Johny | last post by:
Playing a little more with strings, I found out that string.find function provides the position of the first occurance of the substring in the string. Is there a way how to find out all...
4
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that...
5
by: shapper | last post by:
Hello, I have a string which has the following format (I give 2 examples): "FirstWord468x60SecondWord" "FirstWord5x172SecondWord" I need to get the numbers 468 and 60, or 5 and 172 into...
14
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.