match a blank line with RegEx

J

JackRazz

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(matchBlankLine, reOptions)
m = r.Match(rawMsg)
If m.Success
Dim s As String = m.ToString
i = m.Index
mHeaderSection = rawMsg.Substring(0, i)
End If
Catch
MsgBox("oops")
End Try
 
L

Lucvdv

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?

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.RegularExpressions.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).Length = 0" does the trick too.
 
J

JackRazz

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 SetHeaderSectionProp(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.IgnorePatternWhitespace Or
RegexOptions.Multiline) Or RegexOptions.IgnoreCase)
Const matchBlankLine As String = "(\r\n\r\n)+" 'Works "\n\r+"
also works

Try
r = New Regex(matchBlankLine, reOptions1)
m = r.Match(rawMsg)
If m.Success Then
i = m.Index
mHeaderSection = rawMsg.Substring(0, i)
End If
Catch
Throw New System.Exception("matchBlankLine regex didn't work in
eMailMessage.GetHeaderSection.")
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.Substring(0, i)
Exit For
Else
j = j + (ln.Length + 2)
End If
Next
If i <> j Then
Throw New System.Exception("Header line count [end of header] problem in
eMailMessage.GetHeaderSection.")
End If
Return i

End Function
 
J

Jay B. Harlow [MVP - Outlook]

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.Multiline)

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

input = Replace(input, ControlChars.CrLf, 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.Multiline)

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

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.Multiline.

Hope this helps
Jay
 
L

Lucvdv

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

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

The important thing is the RegexOptions.Multiline.

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.
 
J

JackRazz

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

JackRazz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

regex 3
Non matching regex? 1
Regex - Matching URLS 2
Regex pattern match 1
Regex Issues - Finding Qualified URLS 2
Help with Regex 1
Need help with regex 2
I need an workaround for Regex limitation 1

Top