Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to add Regex

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

The following code will open a text file and read each line of text and then search a list box for that line of text, if found it will replace the line of text in the text file with the next item in the list box. (Strange I know)
Code:
Dim lineIn As String
        Dim i As Integer
        Dim listString As String
        Dim arrLines() As String = File.ReadAllLines(TextBox2.Text, System.Text.Encoding.UTF8)
        For j As Integer = 0 To arrLines.Length - 1
            lineIn = arrLines(j)

            
            For i = 0 To ListBox1.Items.Count - 1
                listString = ListBox1.Items.Item(i)
                If InStr(listString, lineIn) Then
                    If i < ListBox1.Items.Count - 1 Then
                        arrLines(j) = ListBox1.Items.Item(i + 1)
                    End If
                End If
            Next i
        Next

        File.WriteAllLines(TextBox2.Text, arrLines, System.Text.Encoding.UTF8)
The code works fine but the problem is, if the line of text in the text file is “sup” the code will pick up any item in the list box that contains the word “sup” (example: supporter, supply, etc..). I have been playing with Regex code so only to pick up the exact word and have the following expression to only pick up the exact word but where in the code do I place it.
Code:
Dim regex As Regex = New Regex(("\b" & lineIn & "\b"))
 
[tt]'If InStr(listString, lineIn) Then
If Regex.IsMatch(listString, "\b" & lineIn & "\b") Then[/tt]
 
Thanks for the code but it is throwing an exception for the REGEX expression
Code:
If regex.IsMatch(listString, "\b" & lineIn & "\b") Then
The exception is:
parsing "\b// An asterisk (*) at the end of a phrase indicates that this line\b" - Quantifier {x,y} following nothing.

This expression worked ouside of this code in a different app, is there something i am missing that is causing this exception.

Thanks
 
If regex is not the way to go, is there another way of picking up the exact text and not variations of it

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top