[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-10-20
6
Medium Priority
?
1,629 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 64

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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