473,573 Members | 3,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DefaultVie w;
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 2835
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*******@disc ussions.microso ft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
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.DefaultVie w;
Regex reg = new Regex(searchThi s,RegexOptions. IgnoreCase);
if (searchBy == "cust")
{
dv.Sort = "Customer ASC";
int i = dv.Find(reg);
if(i > dv.Table.Rows.C ount || i < 0){MessageBox.S how("No file found");}
else {dg.CurrentRowI ndex = 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*******@disc ussions.microso ft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
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.ToS tring());
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.CaseSensit ive = false;
searchThis = "rich";
DataRow[] dr = tCat.Select("Cu stomer 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.Selec t() 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.DefaultVie w;
Regex reg = new Regex(searchThi s,RegexOptions. IgnoreCase);
if (searchBy == "cust")
{
dv.Sort = "Customer ASC";
int i = dv.Find(reg);
if(i > dv.Table.Rows.C ount || i < 0){MessageBox.S how("No file found");}
else {dg.CurrentRowI ndex = 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*******@disc ussions.microso ft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
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*******@disc ussions.microso ft.com> wrote in message
news:B9******** *************** ***********@mic rosoft.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.DefaultVie w;
Regex reg = new Regex(searchThi s,RegexOptions. IgnoreCase);
if (searchBy == "cust")
{
dv.Sort = "Customer ASC";
int i = dv.Find(reg);
if(i > dv.Table.Rows.C ount || i < 0){MessageBox.S how("No file found");}
else {dg.CurrentRowI ndex = 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*******@disc ussions.microso ft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
> 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.DefaultVie w;
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*******@disc ussions.microso ft.com> wrote in message
news:B9******** *************** ***********@mic rosoft.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.DefaultVie w;
Regex reg = new Regex(searchThi s,RegexOptions. IgnoreCase);
if (searchBy == "cust")
{
dv.Sort = "Customer ASC";
int i = dv.Find(reg);
if(i > dv.Table.Rows.C ount || i < 0){MessageBox.S how("No file found");}
else {dg.CurrentRowI ndex = 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*******@disc ussions.microso ft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
> 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*******@disc ussions.microso ft.com> wrote in message
news:CB******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.com> wrote in message
news:B9******** *************** ***********@mic rosoft.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.DefaultVie w;
> Regex reg = new Regex(searchThi s,RegexOptions. IgnoreCase);
> if (searchBy == "cust")
> {
> dv.Sort = "Customer ASC";
> int i = dv.Find(reg);
> if(i > dv.Table.Rows.C ount || i < 0){MessageBox.S how("No file found");}
> else {dg.CurrentRowI ndex = 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*******@disc ussions.microso ft.com> wrote in message
>> news:9A******** *************** ***********@mic rosoft.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.DefaultVie w;
>> > 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
15211
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large,...
7
4585
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 first to make sure it is numeric. Using the is keyword doesn't work (strings cannot be cast as decimal so false is always returned) and catching an...
6
4782
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 string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or...
8
2579
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: @"@?""""|@?"".*?(?!\\).""|''|'.*?(?!\\).'" I am inputting the entire source file string and using it with RegexOptions.Singleline. This works OK with, unless the...
6
5738
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 commands? Like I wouldn't want a string to contain "; Drop TableXYX;" or something along those lines. Thanks, Christian Blackburn
6
1734
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 substring's position ? To explain more, let's suppose mystring='12341' import string
4
7657
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 are NOT the word apple OR banana? Then what would be needed to match only strings that do not CONTAIN the word "apple" or "banana" or "cherry"?
5
1244
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 strings FirstNumber and SecondNumber.
14
2220
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 as the following: "Smith said the dog belongs (or did belong) to the secretary, an employee of the company." Then the list contains the...
0
7733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6338
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.