none
Regex match problem RRS feed

  • Question

  • hey guys im having a problem with the code shown below with m = regex.match i believe i am having a problem with the escape characters, the regex match matches this perfectly ie. "O_P1_Tank" if it is contained in the byte array however "O_P1_Tank (Copy)" is never found by the regex match, I have only just started experimenting with regex so perhaps someone on here could provide some advice as to why i cant get regex.match to work when it includes either of these characters "( )"

    Using br As New BinaryReader(File.Open(TargetFile, FileMode.Open), Encoding.ASCII)
                    Dim ba3 As Byte() = br.ReadBytes(br.BaseStream.Length)

                    m = Regex.Match(Encoding.ASCII.GetString(ba3), (Searchfor)) ', RegexOptions.ExplicitCapture  ''Regex.Unescape  "\b" + (Searchfor) + "\b"
                    If m.Success Then
                        br.BaseStream.Position = m.Index


                        br.BaseStream.Insert(NewValue, Searchfor.Length)
                    Else

                    End If
                End Using

    Regards James

                 
    Friday, May 25, 2018 1:03 AM

Answers

  • Open and close parenthesis, I believe they () are called, are special characters in Regex. You have to use the backslash with them to use them as literals in a search I think it's called.

    This search string works with the previous code I displayed for finding a match with an open and close paren in them.

    Dim searchString As String = "O_P1_Tank \(Copy\).?"

    These characters . $ ^ { [ ( | ) * + ? | all have special meaning in regex so if you want to capture one it has to be preceded by a backslash. The period and question mark mean a single character I believe.

    Regex is not a simple and intuitive ability in my opinion. There are some tutorials which explain O.K. and some that do not. I have a difficult time with it. There are even websites where you can insert text and then create a regex pattern to find the text. However they use typically other languages for performing the Regex match(es) and that does not mean the exact pattern will work in VB.Net for example as typed at the website which may work.

    Also see .NET Regular Expressions and Regular-Expressions.Info although there's other tutorials available that may be better Google search results for "Regex Tutorials".


    La vida loca

    Friday, May 25, 2018 3:21 AM

All replies

  • Please post the search pattern you are currently using, as that will make it easier to provide an edit that accounts for the value in parentheses.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 25, 2018 2:50 AM
    Moderator
  • In the future please insert you code in a Code Block which makes it easier to understand as the Code Block aligns and highlights it nicely.

    I tested you code on a document that contains the word "fun" multiple times and it seemed to find the first occurence fine although I didn't add the change value code you used I just got the match and the base stream position to a message box for test purposes.

    Option Strict On
    
    Imports System.Text.RegularExpressions
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Dim searchString As String = "fun"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using br As New BinaryReader(File.Open("C:\Users\John\Desktop\Regex Test.Txt", FileMode.Open), Encoding.ASCII)
                Dim ba3 As Byte() = br.ReadBytes(CInt(br.BaseStream.Length))
                Dim m = Regex.Match(Encoding.ASCII.GetString(ba3), "\b" & searchString & "\b", RegexOptions.ExplicitCapture)
                If m.Success Then
                    br.BaseStream.Position = m.Index
                    MessageBox.Show(m.ToString & " " & br.BaseStream.Position.ToString)
                Else
    
                End If
            End Using
        End Sub
    
    End Class
    


    La vida loca

    Friday, May 25, 2018 2:54 AM
  • Please post the search pattern you are currently using, as that will make it easier to provide an edit that accounts for the value in parentheses.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    right now im just doing 
    O_P1_Tank (Copy)
    m = Regex.Match(Encoding.ASCII.GetString(ba3), ("O_P1_Tank (Copy)")) 

    I have also tried
    m = Regex.Match(Encoding.ASCII.GetString(ba3), "[a-zA-Z0-9-()!@#$%^&*_ ]*", RegexOptions.ExplicitCapture)

    and 
    m = Regex.Match(Encoding.ASCII.GetString(ba3), ("\b" + "O_P1_Tank (Copy)"  + "\b")) 

    still very new to regex


    Friday, May 25, 2018 2:56 AM
  • In the future please insert you code in a Code Block which makes it easier to understand as the Code Block aligns and highlights it nicely.

    I tested you code on a document that contains the word "fun" multiple times and it seemed to find the first occurence fine although I didn't add the change value code you used I just got the match and the base stream position to a message box for test purposes.

    Option Strict On
    
    Imports System.Text.RegularExpressions
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Dim searchString As String = "fun"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using br As New BinaryReader(File.Open("C:\Users\John\Desktop\Regex Test.Txt", FileMode.Open), Encoding.ASCII)
                Dim ba3 As Byte() = br.ReadBytes(CInt(br.BaseStream.Length))
                Dim m = Regex.Match(Encoding.ASCII.GetString(ba3), "\b" & searchString & "\b", RegexOptions.ExplicitCapture)
                If m.Success Then
                    br.BaseStream.Position = m.Index
                    MessageBox.Show(m.ToString & " " & br.BaseStream.Position.ToString)
                Else
    
                End If
            End Using
        End Sub
    
    End Class


    La vida loca

    will do :)


    Thanks for the prompt reply :)
    Tried it it doesnt work sadly, its exactly what i had as my code earlier, it works on the case for Dim searchString As String = "fun", but it wont work on the case  Dim searchString As String = "fun (Copy)", the " (Copy)" part is the only issue i am having at the moment.

    so for example if the txt file contents is:
    "blah blah junk junk@ nope okayy fun (Copy) haha nice"

    set the search string to ("fun") it will find "fun", but if we set it to find ("fun (Copy)") the matches returns nothing :(

    Friday, May 25, 2018 3:01 AM
  • Open and close parenthesis, I believe they () are called, are special characters in Regex. You have to use the backslash with them to use them as literals in a search I think it's called.

    This search string works with the previous code I displayed for finding a match with an open and close paren in them.

    Dim searchString As String = "O_P1_Tank \(Copy\).?"

    These characters . $ ^ { [ ( | ) * + ? | all have special meaning in regex so if you want to capture one it has to be preceded by a backslash. The period and question mark mean a single character I believe.

    Regex is not a simple and intuitive ability in my opinion. There are some tutorials which explain O.K. and some that do not. I have a difficult time with it. There are even websites where you can insert text and then create a regex pattern to find the text. However they use typically other languages for performing the Regex match(es) and that does not mean the exact pattern will work in VB.Net for example as typed at the website which may work.

    Also see .NET Regular Expressions and Regular-Expressions.Info although there's other tutorials available that may be better Google search results for "Regex Tutorials".


    La vida loca

    Friday, May 25, 2018 3:21 AM
  • Open and close parenthesis, I believe they () are called, are special characters in Regex. You have to use the backslash with them to use them as literals in a search I think it's called.

    This search string works with the previous code I displayed for finding a match with an open and close paren in them.

    Dim searchString As String = "O_P1_Tank \(Copy\).?"


    La vida loca

    Cool that works thanks so much!!

    just one more question before i mark this as answered. i just realized sometimes in my text file it might not always be (Copy), it might be (New) or any other set of characters in between a-z A-z, 0-9 and these characters "!@#$%^&*()-_"
    Friday, May 25, 2018 3:28 AM
  • You want to find anything between an open and close parenthesis? This will find anything between open and close parenthesis if it does not have open or close parenthesis in between two open and close parenthesis.

        Dim searchString As String = "O_P1_Tank \(.*?\).?"

    However if there was alway a space after the last parenthesis that would be easier to capture and disregard. I do not know of a method to match multiple parenthesis in a match in case there were multiple or singles in between an open and close parenthesis.


    La vida loca

    Friday, May 25, 2018 3:40 AM
  • yep :) but i solved it just by doing a find replace before processing it in the regex :)

    Thank you so much for your help your a life saver
    Friday, May 25, 2018 3:43 AM
  • yep :) but i solved it just by doing a find replace before processing it in the regex :)

    Thank you so much for your help your a life saver

    Well I did a career in the United States Coast Guard and always enjoyed the life saving part of the job the best!

    La vida loca

    Friday, May 25, 2018 3:47 AM
  • yep :) but i solved it just by doing a find replace before processing it in the regex :)

    Thank you so much for your help your a life saver


    Well I did a career in the United States Coast Guard and always enjoyed the life saving part of the job the best!

    La vida loca

    OH WOW!, me too!! haah well kinda, I was a volunteer for surf lifesaving on one of the busy beaches down here in Australia for a few months when i lived by the beach :) good times!
    Friday, May 25, 2018 3:53 AM