Solved

RegEx - More Requirements

Posted on 2006-10-23
8
254 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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
Help with preventing selection from a combobox 11 34
MailAddress in vb 4 29
Very Large data in MYSQL 7 73
VB.net SQLCommandBuilder Insert New Records From DataGridView 3 25
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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

829 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