Solved

RegEx - More Requirements

Posted on 2006-10-23
8
255 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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