By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

match a blank line with RegEx

P: n/a
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


Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 30 Jul 2004 03:44:56 -0500, "JackRazz" <Ja******@NotValid.com>
wrote:
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.

Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
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

"JackRazz" <Ja******@NotValid.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #4

P: n/a
On Sat, 31 Jul 2004 14:14:56 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
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.

Nov 20 '05 #5

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

JackRazz
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.