Link to home
Start Free TrialLog in
Avatar of GivenRandy
GivenRandy

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gangwisch
gangwisch

i just use mystring.trim and trimend to accomplish this
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
Avatar of GivenRandy

ASKER

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