Solved

RegEx -- Remove Trailing Spaces on Line

Posted on 2006-10-23
7
1,480 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clone Visual studio 2013 solution 5 24
vb.net checkbox 7 41
Get Top Visible Row of Datagridview 6 27
VB.NET Repostiory Pattern 7 13
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now