match a blank line with RegEx

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

    match a blank line with RegEx

    Anyone know the regular expression to match a blank line where the byte sequence is
    "0D 0A 0D 0A"
    ive tried "\r\n\r\n+" , "^$+" "\n\r" with no success. Any Ideas?

    Thanks - JackRazz


    This is the code fragment I'm trying

    Dim r As Regex, m As Match, i As Integer
    Const matchBlankLine As String = "\n\r"
    'Const matchBlankLine As String = "^$+"

    Try
    r = New Regex(matchBlan kLine, reOptions)
    m = r.Match(rawMsg)
    If m.Success
    Dim s As String = m.ToString
    i = m.Index
    mHeaderSection = rawMsg.Substrin g(0, i)
    End If
    Catch
    MsgBox("oops")
    End Try




  • Lucvdv

    #2
    Re: match a blank line with RegEx

    On Fri, 30 Jul 2004 03:44:56 -0500, "JackRazz" <JackRazz@NotVa lid.com>
    wrote:
    [color=blue]
    > Anyone know the regular expression to match a blank line where the byte sequence is
    > "0D 0A 0D 0A"
    > ive tried "\r\n\r\n+" , "^$+" "\n\r" with no success. Any Ideas?[/color]

    I may be wrong, but I've never known (or believed) that regular expressions
    can be used on a full text at once, AFAIK they're always used a line at a
    time with any newlines (CR *and* LF in windows) stripped.


    Dim re As New System.Text.Reg ularExpressions .Regex("^$")
    Dim s As String = "Line 1" & vbCrLf & vbCrLf & "line 3" & vbCrLf
    Dim ss() As String = Split(s, vbCrLf)
    Dim i As Integer

    For i = LBound(ss) To UBound(ss)
    Debug.WriteLine ("[" & ss(i) & "] - Empty: " & re.Match(ss(i)) .Success)
    Next

    Output:

    [Line 1] - Empty: False
    [] - Empty: True
    [line 3] - Empty: False
    [] - Empty: True

    The 4th result is caused by the presence of a vbCrLf at the end of the
    third line.

    Make sure you use the Split() function, and not the String.Split() method,
    or it won't work. String.Split only looks at the first character of the
    separator, and leaves the LF in the string so the empty lines aren't really
    empty.

    Using regular expressions to detect empty lines this way is a bit moot of
    course, as "ss(i).Leng th = 0" does the trick too.

    Comment

    • JackRazz

      #3
      Re: match a blank line with RegEx

      Lucvdv,

      Thanks for the help. I posted this message really late last night and was exausted
      (and frustrated). I'm working on a POP proxy and lost the incoming message that was
      causing the problem. That reg exp now seems to work. I ended up comparing the Reg
      Exp index to some code like yours to test for differences.

      Hopefully I'll know in a few days if I have a problem. Thanks for the help

      JackRazz



      -------------------------------------------------------------------
      Private Function SetHeaderSectio nProp(ByVal rawMsg As String, ByVal msgLines() As
      String) As Integer
      Dim r As Regex, m As Match, i As Integer, j As Integer
      Const reOptions1 As RegexOptions = ((RegexOptions. IgnorePatternWh itespace Or
      RegexOptions.Mu ltiline) Or RegexOptions.Ig noreCase)
      Const matchBlankLine As String = "(\r\n\r\n) +" 'Works "\n\r+"
      also works

      Try
      r = New Regex(matchBlan kLine, reOptions1)
      m = r.Match(rawMsg)
      If m.Success Then
      i = m.Index
      mHeaderSection = rawMsg.Substrin g(0, i)
      End If
      Catch
      Throw New System.Exceptio n("matchBlankLi ne regex didn't work in
      eMailMessage.Ge tHeaderSection. ")
      End Try

      Dim ln As String
      For Each ln In msgLines
      If ln.Length = 0 Then
      j = j - 2 'Back out the previous vbCrLf
      mHeaderSection = rawMsg.Substrin g(0, i)
      Exit For
      Else
      j = j + (ln.Length + 2)
      End If
      Next
      If i <> j Then
      Throw New System.Exceptio n("Header line count [end of header] problem in
      eMailMessage.Ge tHeaderSection. ")
      End If
      Return i

      End Function


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: match a blank line with RegEx

        Jack,
        You can use the following to find the first blank line in a string.

        Const pattern As String = "^$"
        Dim ex As New Regex(pattern, RegexOptions.Mu ltiline)

        Dim input As String = "Line 1" & ControlChars.Cr Lf &
        ControlChars.Cr Lf & "line 3" & ControlChars.Cr Lf

        input = Replace(input, ControlChars.Cr Lf, ControlChars.Lf )

        Dim match As Match = ex.Match(input)
        If match.Success Then
        Dim s As String = match.ToString
        Dim index As Integer = match.Index
        Dim header As String = input.Substring (0, index)
        End If

        If you don't want the Replace in there you can use:

        Const pattern As String = "\r\n\r\n"
        Dim ex As New Regex(pattern, RegexOptions.Mu ltiline)

        Dim input As String = "Line 1" & ControlChars.Cr Lf &
        ControlChars.Cr Lf & "line 3" & ControlChars.Cr Lf

        Dim match As Match = ex.Match(input)
        If match.Success Then
        Dim s As String = match.ToString
        Dim index As Integer = match.Index
        Dim header As String = input.Substring (0, index)
        End If

        The important thing is the RegexOptions.Mu ltiline.

        Hope this helps
        Jay

        "JackRazz" <JackRazz@NotVa lid.com> wrote in message
        news:O8Om$FhdEH A.712@TK2MSFTNG P09.phx.gbl...[color=blue]
        > Anyone know the regular expression to match a blank line where the byte[/color]
        sequence is[color=blue]
        > "0D 0A 0D 0A"
        > ive tried "\r\n\r\n+" , "^$+" "\n\r" with no success. Any Ideas?
        >
        > Thanks - JackRazz
        >
        >
        > This is the code fragment I'm trying
        >
        > Dim r As Regex, m As Match, i As Integer
        > Const matchBlankLine As String = "\n\r"
        > 'Const matchBlankLine As String = "^$+"
        >
        > Try
        > r = New Regex(matchBlan kLine, reOptions)
        > m = r.Match(rawMsg)
        > If m.Success
        > Dim s As String = m.ToString
        > i = m.Index
        > mHeaderSection = rawMsg.Substrin g(0, i)
        > End If
        > Catch
        > MsgBox("oops")
        > End Try
        >
        >
        >
        >[/color]


        Comment

        • Lucvdv

          #5
          Re: match a blank line with RegEx

          On Sat, 31 Jul 2004 14:14:56 -0500, "Jay B. Harlow [MVP - Outlook]"
          <Jay_Harlow_MVP @msn.com> wrote:
          [color=blue]
          > input = Replace(input, ControlChars.Cr Lf, ControlChars.Lf )[/color]

          I hadn't thought of translating the text to unix format ;-)

          [color=blue]
          > The important thing is the RegexOptions.Mu ltiline.[/color]

          And I hadn't even noticed that options exists, so I was obviously wrong in
          thinking it could only be done line by line.

          My experience with regexps is limited to *n*x and message filters in Forté
          Agent, and in *n*x it isn't too deep, but I don't think such an option
          exist in either one.

          Comment

          • JackRazz

            #6
            Re: match a blank line with RegEx

            Lucvd and Jay,
            I got working now. Regular expressions definately take some getting used to. Thanks
            for the help.

            JackRazz


            Comment

            Working...