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
Solved

RegEx -- Remove Trailing Spaces on Line

Posted on 2006-10-23
7
1,483 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 28
Generate Numbers in JQuery file 11 66
Complex SQL statement in VB.NET 7 31
VB.NET - Refactor Class per SOLID principles 2 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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …

809 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