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

Posted on 2006-10-20
Medium Priority
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.
Question by:GivenRandy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 63

Accepted Solution

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)

LVL 10

Expert Comment

ID: 17777339
i just use mystring.trim and trimend to accomplish this
LVL 63

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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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.
LVL 63

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;


Author Comment

ID: 17789037

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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