473,701 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Nov 20 '05 #1
5 10181
On Fri, 30 Jul 2004 03:44:56 -0500, "JackRazz" <Ja******@NotVa lid.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.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.

Nov 20 '05 #2
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
Nov 20 '05 #3
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" <Ja******@NotVa lid.com> wrote in message
news:O8******** *****@TK2MSFTNG P09.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(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

Nov 20 '05 #4
On Sat, 31 Jul 2004 14:14:56 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************ @msn.com> wrote:
input = Replace(input, ControlChars.Cr Lf, ControlChars.Lf )
I hadn't thought of translating the text to unix format ;-)

The important thing is the RegexOptions.Mu ltiline.


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12023
by: Dave Sisk | last post by:
Hi Folks: I need the right regex to match either a comment line (--) or a blank line. What I've got below doesn't seem to be working like I'd expect. I've posted a snippet of what I have below...would anyone be kind enough to point me in the right direction? I also apparently need to strip the ';' off the end of the INSERT statement... TIA, Dave
3
18751
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". Therefore some matches are: XYZAHIJ ABC
3
5299
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples of what I have tried... string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf"; while(true) { Regex .Match(testString,@"(\w)"); } ---------------------------------------------------------------------- string testString = "lkf...
1
7399
by: Laser Lu | last post by:
Hi, all, I'm now writing a program to compress JavaScript code. One puzzle is how to write a regular expression to find out and remove all the redundent blank spaces. However, those blank spaces that are in the comments should be kept intact. I've tried to write some Regexs, and I list them here for your information: regex = new Regex(@"/\**?\*/"); // pattern used to match a multiline comment block regex = new Regex(@"//*\n"); //...
1
5151
by: DKode | last post by:
I have the following regex that I am using: Match match = Regex.Match(global, "%(?<column>.+?)%"); my input string (global) is this: "something\%username%\somethingelse\%match2%\some\%match3% I want it to match "username","match2" and "match3" but it only matches %username%, it doesnt continue on and get the other
4
2640
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line option and I've also tried stripping out the VbCr
11
11817
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines that has the word 'event' and the word 'new'. Note that i need lines that has both the words only and not either one of them..
3
2619
by: Hrvoje Niksic | last post by:
I often have the need to match multiple regexes against a single string, typically a line of input, like this: if (matchobj = re1.match(line)): ... re1 matched; do something with matchobj ... elif (matchobj = re2.match(line)): ... re2 matched; do something with matchobj ... elif (matchobj = re3.match(line)): .....
14
4983
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7827
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4411
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4665
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3104
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.