Exact match and variable

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Exact match and variable

Post by Liverpool »

Hi

I have two questions

1. I am using a regex to search a VB.NET listbox items, the regex expression will pick up all items but i would like it only to find the exact match.

Here is the code iam using:

Code: Select all

Dim iq As Integer
        For iq = 0 To ListBox1.Items.Count - 1
Dim RO As String = (ListBox1.Items(iq).ToString)
                     
            Dim regex As Regex = New Regex("sum") 
            Dim input As String = iq
 
            If InStr(RO, regex.ToString) Then
                'do something
next
This works fine but how do i get it to only pick up the exact text (sum) and anot any items that contains variations of (sum) Example= (summer, summons, etc...)

My second question is about assigning a variable to the expression (text to search for).

can i chnage the code to:

Code: Select all

Dim iq As Integer
        For iq = 0 To ListBox1.Items.Count - 1
Dim RO As String = (ListBox1.Items(iq).ToString)
             Dim str1 as string = "sum"        
            Dim regex As Regex = New Regex(str1) 
            Dim input As String = iq
 
            If InStr(RO, regex.ToString) Then
                'do something
next
and how do i change the expression to only search for the exact text as like question 1

Thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Exact match and variable

Post by prometheuzz »

I am unfamiliar with VB's regex engine, but in most regex engine's I've "played" with, the ^ and $ are meta characters denoting the beginning and the ending of an input string.
So, you could try using the regex "^sum$" instead of "sum".

HTH
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Re: Exact match and variable

Post by Liverpool »

Unfortunately it does not work.

From what i have seen on the net "\b string \b" is what is used in VB.NET but it will not work for me
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Exact match and variable

Post by prometheuzz »

Liverpool wrote:Unfortunately it does not work.

From what i have seen on the net "\b string \b" is what is used in VB.NET but it will not work for me
In your original post, you are using the function/method InStr, which seems to be some sort of string operation, not a regex-test.
Does this work?

Code: Select all

If Regex.IsMatch(RO, "^sum$") Then
  ' do something
and/or

Code: Select all

Dim m As Match = System.Text.RegularExpressions.Regex.Match(RO, "^sum$")
 
If m.Success Then
  ' do something

More info on .NET regex:
http://www.regular-expressions.info/dotnet.html
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Re: Exact match and variable

Post by Liverpool »

Thanks alot, it works when i use the If Regex.IsMatch.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Exact match and variable

Post by prometheuzz »

Liverpool wrote:Thanks alot, it works when i use the If Regex.IsMatch.
Good to hear it & you're welcome!
Post Reply