[Regular Expression] extraction when bounds are vbCr and vbLf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • teo

    #1

    [Regular Expression] extraction when bounds are vbCr and vbLf

    Hallo

    I need to extract a subtext from a text.
    The subtext must contain a given word.

    The subtext bounds are:

    vbCr (return)
    vbLf (new line)
    vbCrLf (return+new line)
    the very beginning of the text
    the very ending of the text


    I tried with:

    ^
    \n
    \r
    $

    so to have:

    Dim myText As String
    Dim myPattern As String = "^\n\r" & myWord & "\n\r$"

    Dim match As Match = Regex.Match(myT ext, myPattern, RegexOptions.Mu ltiline
    Or RegexOptions.Ig noreCase)

    but I had problems.
  • Chris

    #2
    Re: [Regular Expression] extraction when bounds are vbCr and vbLf

    Try this, where Text is the subtext

    Dim FoundMatch As Boolean
    Try
    FoundMatch = Regex.IsMatch(S ubjectString, "^$Text$",
    RegexOptions.Mu ltiline)
    Catch ex As ArgumentExcepti on
    'Syntax error in the regular expression
    End Try

    HTH

    Chris

    "teo" <teo@inwind.itw rote in message
    news:gparf293bi klfnpte16i7707g hi3n83v8i@4ax.c om...
    Hallo
    >
    I need to extract a subtext from a text.
    The subtext must contain a given word.
    >
    The subtext bounds are:
    >
    vbCr (return)
    vbLf (new line)
    vbCrLf (return+new line)
    the very beginning of the text
    the very ending of the text
    >
    >
    I tried with:
    >
    ^
    \n
    \r
    $
    >
    so to have:
    >
    Dim myText As String
    Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
    >
    Dim match As Match = Regex.Match(myT ext, myPattern, RegexOptions.Mu ltiline
    Or RegexOptions.Ig noreCase)
    >
    but I had problems.

    Comment

    • Chris

      #3
      Re: [Regular Expression] extraction when bounds are vbCr and vbLf

      Hi Teo,

      Just to clarify, are you trying to find all the lines in a given file that
      contain a particular word?

      What does your data look like, are these strictly text files? Can you give
      me an example that I can test on. Where ever there is a VbLf, VbCr, or
      VbCrLf you can just make note of it

      This is some text VbCrLf
      that I want to test VbCrLf
      against.

      Regulazy or the Regulator by Roy Osherove might help as well.


      Chris

      "teo" <teo@inwind.itw rote in message
      news:gparf293bi klfnpte16i7707g hi3n83v8i@4ax.c om...
      Hallo
      >
      I need to extract a subtext from a text.
      The subtext must contain a given word.
      >
      The subtext bounds are:
      >
      vbCr (return)
      vbLf (new line)
      vbCrLf (return+new line)
      the very beginning of the text
      the very ending of the text
      >
      >
      I tried with:
      >
      ^
      \n
      \r
      $
      >
      so to have:
      >
      Dim myText As String
      Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
      >
      Dim match As Match = Regex.Match(myT ext, myPattern, RegexOptions.Mu ltiline
      Or RegexOptions.Ig noreCase)
      >
      but I had problems.

      Comment

      • teo

        #4
        Re: [Regular Expression] extraction when bounds are vbCr and vbLf

        I uploaded a zip file (2 Kb) that contains a .rtf file
        with the explanation and a sample,
        here:
        The Zshare file hosting providing website had been shut down many few years ago. The new zShare website is a news blog that publishes the latest trending news on the web. So what happened to the old Zshare? As the new owner of the domain zshare.net, I honestly don’t know. The domain had not been

        (no java required)


        >Hi Teo,
        >
        >Just to clarify, are you trying to find all the lines in a given file that
        >contain a particular word?
        >
        >What does your data look like, are these strictly text files? Can you give
        >me an example that I can test on. Where ever there is a VbLf, VbCr, or
        >VbCrLf you can just make note of it
        >
        >This is some text VbCrLf
        >that I want to test VbCrLf
        >against.
        >
        >Regulazy or the Regulator by Roy Osherove might help as well.
        >http://tools.osherove.com/
        >
        >Chris
        >
        >"teo" <teo@inwind.itw rote in message
        >news:gparf293b iklfnpte16i7707 ghi3n83v8i@4ax. com...
        >Hallo
        >I need to extract a subtext from a text.
        >The subtext must contain a given word.
        >>
        >The subtext bounds are:
        >>
        >vbCr (return)
        >vbLf (new line)
        >vbCrLf (return+new line)
        >the very beginning of the text
        >the very ending of the text
        >>
        >>
        >I tried with:
        >>
        >^
        >\n
        >\r
        >$
        >>
        >so to have:
        >>
        >Dim myText As String
        >Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
        >>
        >Dim match As Match = Regex.Match(myT ext, myPattern, RegexOptions.Mu ltiline
        >Or RegexOptions.Ig noreCase)
        >>
        >but I had problems.
        >

        Comment

        • Chris

          #5
          Re: [Regular Expression] extraction when bounds are vbCr and vbLf

          Hi Teo,

          Thanks for putting that up there. It helped nicely.


          Try the following code:

          Imports System.Text.Reg ularExpressions
          Imports System.Windows. Forms
          Imports System.IO
          Public Module Module1

          Public Sub main()
          Dim fileName As String = InputBox("Give me the file to parse", _
          "File name input box")
          CheckContents(f ileName)

          End Sub

          ''' <summary>
          ''' Check the contents of a file
          ''' </summary>
          ''' <param name="Filename" ></param>
          ''' <remarks>Coul d be expanded to check against multiple
          ''' keywords by adding another argument that contains the
          ''' keyword and inserting it in place of the DIO characters</remarks>
          Public Sub CheckContents(B yVal Filename As String)

          'Declare RegExp
          Dim dioRegex As New Regex(".*DIO.*( \n|\r|\r\n)",
          RegexOptions.Ig noreCase)

          'Make sure the file is really there
          Dim fileExists As Boolean
          fileExists = My.Computer.Fil eSystem.FileExi sts(Filename)

          'Throw exception if the file is not there
          If Not fileExists Then Throw New FileNotFoundExc eption

          'Get the contents of the file
          Dim fileContents As String
          fileContents = My.Computer.Fil eSystem.ReadAll Text(Filename)

          'Check File Contents Against Regex
          Dim dioMatches As MatchCollection = dioRegex.Matche s(fileContents)

          'Loop though all of the matches and do something cool with them
          For Each dioMatch As Match In dioMatches

          'Your cool code goes here :o)

          'I'm just going to print the results to a messagebox
          MsgBox(dioMatch .Value)

          Next

          End Sub
          End Module


          Please keep in mind that some of the RTF formatting characters are left. I
          didn't know if you wanted them left in, but you should be able to easily
          strip out the /p and other character combinations using Str.Replace(old Char,
          newChar) where Str is the your data.

          Best regards,

          Chris


          "teo" <teo@inwind.itw rote in message
          news:ljurf21nv1 9sgmncmaca6i5fo brot9pv4r@4ax.c om...
          >I uploaded a zip file (2 Kb) that contains a .rtf file
          with the explanation and a sample,
          here:
          The Zshare file hosting providing website had been shut down many few years ago. The new zShare website is a news blog that publishes the latest trending news on the web. So what happened to the old Zshare? As the new owner of the domain zshare.net, I honestly don’t know. The domain had not been

          (no java required)
          >
          >
          >
          >>Hi Teo,
          >>
          >>Just to clarify, are you trying to find all the lines in a given file that
          >>contain a particular word?
          >>
          >>What does your data look like, are these strictly text files? Can you give
          >>me an example that I can test on. Where ever there is a VbLf, VbCr, or
          >>VbCrLf you can just make note of it
          >>
          >>This is some text VbCrLf
          >>that I want to test VbCrLf
          >>against.
          >>
          >>Regulazy or the Regulator by Roy Osherove might help as well.
          >>http://tools.osherove.com/
          >>
          >>Chris
          >>
          >>"teo" <teo@inwind.itw rote in message
          >>news:gparf293 biklfnpte16i770 7ghi3n83v8i@4ax .com...
          >>Hallo
          >>I need to extract a subtext from a text.
          >>The subtext must contain a given word.
          >>>
          >>The subtext bounds are:
          >>>
          >>vbCr (return)
          >>vbLf (new line)
          >>vbCrLf (return+new line)
          >>the very beginning of the text
          >>the very ending of the text
          >>>
          >>>
          >>I tried with:
          >>>
          >>^
          >>\n
          >>\r
          >>$
          >>>
          >>so to have:
          >>>
          >>Dim myText As String
          >>Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
          >>>
          >>Dim match As Match = Regex.Match(myT ext, myPattern,
          >>RegexOptions. Multiline
          >>Or RegexOptions.Ig noreCase)
          >>>
          >>but I had problems.
          >>
          >

          Comment

          • teo

            #6
            Re: [Regular Expression] extraction when bounds are vbCr and vbLf

            I made few tests and I faced one problem:

            the last sentence is never matched

            (that is
            if the word is in the last sentence
            I'm not able to extract the sentence;
            while if it is in the first sentence, it is all OK...)



            >Hi Teo,
            >
            >Thanks for putting that up there. It helped nicely.
            >
            >
            >Try the following code:
            >
            >Imports System.Text.Reg ularExpressions
            >Imports System.Windows. Forms
            >Imports System.IO
            >Public Module Module1
            >
            Public Sub main()
            Dim fileName As String = InputBox("Give me the file to parse", _
            "File name input box")
            CheckContents(f ileName)
            >
            End Sub
            >
            ''' <summary>
            ''' Check the contents of a file
            ''' </summary>
            ''' <param name="Filename" ></param>
            ''' <remarks>Coul d be expanded to check against multiple
            ''' keywords by adding another argument that contains the
            ''' keyword and inserting it in place of the DIO characters</remarks>
            Public Sub CheckContents(B yVal Filename As String)
            >
            'Declare RegExp
            Dim dioRegex As New Regex(".*DIO.*( \n|\r|\r\n)",
            >RegexOptions.I gnoreCase)
            >
            'Make sure the file is really there
            Dim fileExists As Boolean
            fileExists = My.Computer.Fil eSystem.FileExi sts(Filename)
            >
            'Throw exception if the file is not there
            If Not fileExists Then Throw New FileNotFoundExc eption
            >
            'Get the contents of the file
            Dim fileContents As String
            fileContents = My.Computer.Fil eSystem.ReadAll Text(Filename)
            >
            'Check File Contents Against Regex
            Dim dioMatches As MatchCollection = dioRegex.Matche s(fileContents)
            >
            'Loop though all of the matches and do something cool with them
            For Each dioMatch As Match In dioMatches
            >
            'Your cool code goes here :o)
            >
            'I'm just going to print the results to a messagebox
            MsgBox(dioMatch .Value)
            >
            Next
            >
            End Sub
            >End Module
            >
            >
            >Please keep in mind that some of the RTF formatting characters are left. I
            >didn't know if you wanted them left in, but you should be able to easily
            >strip out the /p and other character combinations using Str.Replace(old Char,
            >newChar) where Str is the your data.
            >
            >Best regards,
            >
            >Chris
            >
            >
            >"teo" <teo@inwind.itw rote in message
            >news:ljurf21nv 19sgmncmaca6i5f obrot9pv4r@4ax. com...
            >>I uploaded a zip file (2 Kb) that contains a .rtf file
            >with the explanation and a sample,
            >here:
            >http://www.zshare.net/download/regexsmp-zip.html
            >(no java required)
            >>
            >>
            >>
            >>>Hi Teo,
            >>>
            >>>Just to clarify, are you trying to find all the lines in a given file that
            >>>contain a particular word?
            >>>
            >>>What does your data look like, are these strictly text files? Can you give
            >>>me an example that I can test on. Where ever there is a VbLf, VbCr, or
            >>>VbCrLf you can just make note of it
            >>>
            >>>This is some text VbCrLf
            >>>that I want to test VbCrLf
            >>>against.
            >>>
            >>>Regulazy or the Regulator by Roy Osherove might help as well.
            >>>http://tools.osherove.com/
            >>>
            >>>Chris
            >>>
            >>>"teo" <teo@inwind.itw rote in message
            >>>news:gparf29 3biklfnpte16i77 07ghi3n83v8i@4a x.com...
            >>>Hallo
            >>>I need to extract a subtext from a text.
            >>>The subtext must contain a given word.
            >>>>
            >>>The subtext bounds are:
            >>>>
            >>>vbCr (return)
            >>>vbLf (new line)
            >>>vbCrLf (return+new line)
            >>>the very beginning of the text
            >>>the very ending of the text
            >>>>
            >>>>
            >>>I tried with:
            >>>>
            >>>^
            >>>\n
            >>>\r
            >>>$
            >>>>
            >>>so to have:
            >>>>
            >>>Dim myText As String
            >>>Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
            >>>>
            >>>Dim match As Match = Regex.Match(myT ext, myPattern,
            >>>RegexOptions .Multiline
            >>>Or RegexOptions.Ig noreCase)
            >>>>
            >>>but I had problems.
            >>>
            >>
            >

            Comment

            • Chris

              #7
              Re: [Regular Expression] extraction when bounds are vbCr and vbLf

              Hi Teo,

              I missed the case if there is not a line feed, carriage return or
              combination.

              Try replacing the dioRegex, in the CheckContents sub, with the following:

              Dim dioRegex As New Regex(".*DIO.*( (\n|\r|\r\n)|.* )",
              RegexOptions.Ig noreCase)

              Hope that helps,

              Chris


              "teo" <teo@inwind.itw rote in message
              news:t1ntf2divn v4c9s6r3slb95nn irin3aq1p@4ax.c om...
              >I made few tests and I faced one problem:
              >
              the last sentence is never matched
              >
              (that is
              if the word is in the last sentence
              I'm not able to extract the sentence;
              while if it is in the first sentence, it is all OK...)
              >
              >
              >
              >
              >>Hi Teo,
              >>
              >>Thanks for putting that up there. It helped nicely.
              >>
              >>
              >>Try the following code:
              >>
              >>Imports System.Text.Reg ularExpressions
              >>Imports System.Windows. Forms
              >>Imports System.IO
              >>Public Module Module1
              >>
              > Public Sub main()
              > Dim fileName As String = InputBox("Give me the file to parse", _
              > "File name input box")
              > CheckContents(f ileName)
              >>
              > End Sub
              >>
              > ''' <summary>
              > ''' Check the contents of a file
              > ''' </summary>
              > ''' <param name="Filename" ></param>
              > ''' <remarks>Coul d be expanded to check against multiple
              > ''' keywords by adding another argument that contains the
              > ''' keyword and inserting it in place of the DIO characters</remarks>
              > Public Sub CheckContents(B yVal Filename As String)
              >>
              > 'Declare RegExp
              > Dim dioRegex As New Regex(".*DIO.*( \n|\r|\r\n)",
              >>RegexOptions. IgnoreCase)
              >>
              > 'Make sure the file is really there
              > Dim fileExists As Boolean
              > fileExists = My.Computer.Fil eSystem.FileExi sts(Filename)
              >>
              > 'Throw exception if the file is not there
              > If Not fileExists Then Throw New FileNotFoundExc eption
              >>
              > 'Get the contents of the file
              > Dim fileContents As String
              > fileContents = My.Computer.Fil eSystem.ReadAll Text(Filename)
              >>
              > 'Check File Contents Against Regex
              > Dim dioMatches As MatchCollection = dioRegex.Matche s(fileContents)
              >>
              > 'Loop though all of the matches and do something cool with them
              > For Each dioMatch As Match In dioMatches
              >>
              > 'Your cool code goes here :o)
              >>
              > 'I'm just going to print the results to a messagebox
              > MsgBox(dioMatch .Value)
              >>
              > Next
              >>
              > End Sub
              >>End Module
              >>
              >>
              >>Please keep in mind that some of the RTF formatting characters are left. I
              >>didn't know if you wanted them left in, but you should be able to easily
              >>strip out the /p and other character combinations using
              >>Str.Replace(o ldChar,
              >>newChar) where Str is the your data.
              >>
              >>Best regards,
              >>
              >>Chris
              >>
              >>
              >>"teo" <teo@inwind.itw rote in message
              >>news:ljurf21n v19sgmncmaca6i5 fobrot9pv4r@4ax .com...
              >>>I uploaded a zip file (2 Kb) that contains a .rtf file
              >>with the explanation and a sample,
              >>here:
              >>http://www.zshare.net/download/regexsmp-zip.html
              >>(no java required)
              >>>
              >>>
              >>>
              >>>>Hi Teo,
              >>>>
              >>>>Just to clarify, are you trying to find all the lines in a given file
              >>>>that
              >>>>contain a particular word?
              >>>>
              >>>>What does your data look like, are these strictly text files? Can you
              >>>>give
              >>>>me an example that I can test on. Where ever there is a VbLf, VbCr, or
              >>>>VbCrLf you can just make note of it
              >>>>
              >>>>This is some text VbCrLf
              >>>>that I want to test VbCrLf
              >>>>against.
              >>>>
              >>>>Regulazy or the Regulator by Roy Osherove might help as well.
              >>>>http://tools.osherove.com/
              >>>>
              >>>>Chris
              >>>>
              >>>>"teo" <teo@inwind.itw rote in message
              >>>>news:gparf2 93biklfnpte16i7 707ghi3n83v8i@4 ax.com...
              >>>>Hallo
              >>>>I need to extract a subtext from a text.
              >>>>The subtext must contain a given word.
              >>>>>
              >>>>The subtext bounds are:
              >>>>>
              >>>>vbCr (return)
              >>>>vbLf (new line)
              >>>>vbCrLf (return+new line)
              >>>>the very beginning of the text
              >>>>the very ending of the text
              >>>>>
              >>>>>
              >>>>I tried with:
              >>>>>
              >>>>^
              >>>>\n
              >>>>\r
              >>>>$
              >>>>>
              >>>>so to have:
              >>>>>
              >>>>Dim myText As String
              >>>>Dim myPattern As String = "^\n\r" & myWord & "\n\r$"
              >>>>>
              >>>>Dim match As Match = Regex.Match(myT ext, myPattern,
              >>>>RegexOption s.Multiline
              >>>>Or RegexOptions.Ig noreCase)
              >>>>>
              >>>>but I had problems.
              >>>>
              >>>
              >>
              >

              Comment

              • teo

                #8
                Re: [Regular Expression] extraction when bounds are vbCr and vbLf

                It seems all ok,
                thanks.

                Comment

                Working...