Solved

RegEx -- Remove Trailing Spaces on Line

Posted on 2006-10-23
7
1,481 Views
Last Modified: 2010-08-05
Based on similar question:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22032483.html

I want it to also remove trailing spaces on a line. The accepted answer might have this initialization instead:

        Dim originalText As String = vbCrLf & "Second line." & vbCrLf & _
            "  Third line, with leading space and trailing spaces.      " & vbCrLf & _
            vbCrLf & "Fifth line." & vbCrLf

How would it be, now?
0
Comment
Question by:GivenRandy
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17789205
Hi GivenRandy;

Try it this way. It will remove all white space before and at the end of each line.

        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>.*?)\s*$", _
            "$3" & vbCrLf, RegexOptions.Multiline)
        MessageBox.Show("-->" & cleanedText & "<--")

Fernando
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17789317
And to also remove the blank lines?
0
 
LVL 62

Accepted Solution

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

Sorry about that but I overlooked the replacement string when I adjusted the Regex pattern string. So replace this

    "$3" & vbCrLf

With this

    "$3"

Or copy and past this line into the code replacing the last one I posted.

        Dim cleanedText As String = Regex.Replace(originalText, _
            "^(?<1>\r\n)*(?<2>\s*)(?<3>.*?)\s*$", _
            "$3", RegexOptions.Multiline)

Fernando

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Author Comment

by:GivenRandy
ID: 17789667
Works precisely. Thanks!

By the way, I picked up the book "Master Regular Expressions" so that maybe I can improve my RegEx skills more quickly. Thanks for your help!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17789692
Not a problem, glad I was able to help. ;=)
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17790137
I'm gonna have one more, with all of them laid out. I'm getting the hang of it, but still not getting all of them at once.
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17790187
One more:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22034559.html

You have been doing them correctly, but I keep adding specs (spec creep).
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 69
More on Time zones in vb 2010 12 38
VB.NET 1 21
VB.NET 2008 Publish Error 2 25
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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