473,695 Members | 2,227 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 10179
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
18749
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
7397
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
2618
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
4981
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
8638
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8998
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...
1
8857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7668
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
6494
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
4586
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2278
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1979
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.