Solved

RegEx - More Requirements

Posted on 2006-10-23
8
253 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 63

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 63

Expert Comment

by:Fernando Soto
ID: 17790308
You do not need to replace \t with any other character?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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
 
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 63

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 63

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 49
Exit the loop 4 43
VB.NET 2008 Populate DataModel with DataTable 44 32
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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