Solved

RegEx -- Remove Trailing Spaces on Line

Posted on 2006-10-23
7
1,491 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
[X]
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
  • 4
  • 3
7 Comments
 
LVL 63

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 63

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 63

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

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!

Question has a verified solution.

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
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: …

726 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