Solved

RegEx - More Requirements

Posted on 2006-10-23
8
251 Views
Last Modified: 2010-05-18
Releated to earlier question(s):

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

Starting fresh, with some additional requirements. There are a bunch of lines, ending with with "\r\n", that are in a long string. The replacement string needs to:

+ remove "//" and anything after it (could also see just "//\r\n", with no text on line after the "//")
+ remove tab ("\t") characters
+ remove blank lines (either with OR without whitespace before the "\r\n")
+ remove leading whitespace at beginning of line
+ remove trailing whitespace at end of line
0
Comment
Question by:GivenRandy
  • 4
  • 4
8 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17790256
Hi GivenRandy;

Whe you say, "remove tab ("\t") characters", That is all \t in the string data?

Fernando
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17790274
Right, all of the \t in the whole string. Just delete them.

Right now, I have like 5 lines of code to do this and trying to reduce to one. Not just for analysis-paralysis, but to help learn RegEx in the background. :)
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17790308
You do not need to replace \t with any other character?
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17790389
Correct. It gets stripped. It was a carryover from a tab-delimited file, but the tabs are not significant -- it now is space separated and newlines.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Author Comment

by:GivenRandy
ID: 17790404
Something like this at the moment:

        cleanedText = Regex.Replace(cleanedText, "[\t]", String.Empty)

I used the brackets because there had been other characters in the set as well. Now, just the tab.
0
 
LVL 62

Accepted Solution

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

The only way that I can come up with a pattern that you want without making multiple regex calls is the following.

        ' I took the pattern out of the call itself to make the code more readable
        Dim pattern As String = "^\s*\r\n|^\s*//.*?\r\n|^(?<1>\r\n)*\s*(?<3>.*?)" & _
            "\s*//.*?(?<4>\r\n)|^(?<1>\r\n)*\s*(?<3>.*?)(?<4>\r\n)"

        Dim cleanedText As String = Regex.Replace(originalText, pattern, "$3$4", _
            RegexOptions.Multiline).Replace(Chr(9), "")

As you can see from the call to parse the string the replacement of the tab char is done after the string is returned from regex.

Fernando
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 17794914
Touche!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17795583
Glad I was able to help. ;=)
0

Featured Post

Highfive Gives IT Their Time Back

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

9 Experts available now in Live!

Get 1:1 Help Now