Solved

RegEx -- Delete Spaces at Beginning of Line, and Empty Lines

Posted on 2006-10-20
6
1,598 Views
Last Modified: 2012-06-22
I have some original text that I am filtering using RegEx. Most stuff works like normal, but I'm having a hard time simulating a Trim() and also deleting empty lines. I want to use RegEx for this approach (i.e., not split and trim). In the following the "\r\n" is the standard ControlChars.CrLf characters.

 originalText = "\r\nSecond line.\r\n Third line, with leading space.\r\n\r\nFifth line.\r\n"

I can remove the double CrLf in the middle:

 cleanedText = Regex.Replace(originalText, "\r\n\r\n", ControlChars.CrLf)

But, how about the first one?

How about deleting the extra space in front of "Third line".

Looking for generic approach, not just for this example.
0
Comment
Question by:GivenRandy
  • 3
  • 2
6 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17777257
Hi GivenRandy;

This should do what you want.

        Dim originalText As String = vbCrLf & "Second line." & vbCrLf & _
            "  Third line, with leading space." & vbCrLf & _
            vbCrLf & "Fifth line." & vbCrLf
        Dim cleanedText As String = Regex.Replace(originalText, _
            "^(?<1>\r\n)*(?<2>\s*)(?<3>[^\r]+)(?<4>\r\n)*", _
            "$3" & vbCrLf, RegexOptions.Multiline)
        MessageBox.Show(cleanedText)

Fernando
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17777339
i just use mystring.trim and trimend to accomplish this
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17777424
Hi gangwisch;

GivenRandy also wants to remove all blank line from with in the string as well as leading spaces on lines with in the string. So then he would have to split the string loop through the array remove blank lines and make sure there is only one set of CrLf at the end of the line. The Regex will do it in one step.

Fernando
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Author Comment

by:GivenRandy
ID: 17779649
Seems to work. Did you find a particular tool and/or book to be helpful? I know the basics of Regex, but need to improve.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17781359
Hi GivenRandy;

Sorry but I have not found any good book on regular expressions. When I started working with Regex in .Net I used a program called the Regulator. This program is available for download at http://sourceforge.net/projects/regulator/ . Since then I have written my own and have been using it, a project that I am still working on.

Good luck;

Fernando
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17789037
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now