Solved

RegEx -- Remove Trailing Spaces on Line

Posted on 2006-10-23
7
1,479 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now