ASP.NET/VB/REGEX: Format special content in columns

Using ASP.NET/VB and Regualar Expressions, how can I change this:
Hello World

===Row 1 Column 1
===Row 1 Column 2
===Row 1 Column 3

===Row 2 Column 1
===Row 2 Column 2
===Row 2 Column 3

Hello Again World

Open in new window

to this:
Hello World
<span class="infoColumns">
 <span class="col1">Row 1 Column 1</span>
 <span class="col2">Row 1 Column 2</span>
 <span class="col3">Row 1 Column 3</span>
<span class="infoColumns">
 <span class="col1">Row 2 Column 1</span>
 <span class="col2">Row 2 Column 2</span>
 <span class="col3">Row 2 Column 3</span>
Hello Again World

Open in new window

LVL 16
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi hankknight;

Given the input in the question the following code snippet should give you what you are looking for.

// Read all lines of the file into a string array or just start out with string array
var input = File.ReadAllLines("C:/Working Directory/TestData.txt");
// Used to build the output string
StringBuilder output = new StringBuilder();
// Used to place the ending </span> in the output string
bool ended = true;

foreach (var line in input)
    if (line.StartsWith("==="))
        if (ended)
            output.Append("<span class=\"infoColumns\">\r\n");
            ended = false;
        var fields = line.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        var strFormatted = String.Format("\t<span class=\"col{0}\">Row {1} Column {2}</span>\r\n", fields[3], fields[1], fields[3]);
    else if (line.Count() > 0)
        ended = true; 


Open in new window

hankknightAuthor Commented:
I want this to be done using REGEX and I need to to work with VB, not C#.
käµfm³d 👽Commented:

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim source As String = "Hello World" + vbCrLf +
                               "" + vbCrLf +
                               "===Row 1 Column 1" + vbCrLf +
                               "===Row 1 Column 2" + vbCrLf +
                               "===Row 1 Column 3" + vbCrLf +
                               "" + vbCrLf +
                               "===Row 2 Column 1" + vbCrLf +
                               "===Row 2 Column 2" + vbCrLf +
                               "===Row 2 Column 3" + vbCrLf +
                               "" + vbCrLf +
                               "Hello Again World"
        Dim replacement As String = "<span class=""infoColumns"">" + vbCrLf +
                                    "    <span class=""col$2"">$1</span>" + vbCrLf +
                                    "    <span class=""col$4"">$3</span>" + vbCrLf +
                                    "    <span class=""col$6"">$5</span>" + vbCrLf +
        Dim pattern As String = "(?<=\n)[ \t]*\r\n===(Row \d Column (\d))\r\n===(Row \d Column (\d))\r\n===(Row \d Column (\d))\r\n"

        Dim result As String = Regex.Replace(source, pattern, replacement)

        result = result.Replace("</span><span", "</span>" + vbCrLf + "<span")

    End Sub

End Module

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

hankknightAuthor Commented:
I get an error:
Compiler Error Message: BC30516: Overload resolution failed because no accessible 'Replace' accepts this number of arguments.
Dim result As String = Regex.Replace(source, pattern, replacement)

Open in new window

käµfm³d 👽Commented:
I'd have to see your full code. What I posted above is working code.

Make sure you have the Imports statement at the top of your code file.
hankknightAuthor Commented:
Thanks,  Could you please make it work with this code?
<%@ Page Language="VB" %>

Dim input As String = "{-} {c} {tm} {r}" & VbNewLine & "Hello World" & VbNewLine & VbNewLine & "===Row 1 Column 1" & VbNewLine & "===Row 1 Column 2" & VbNewLine & "===Row 1 Column 3" & VbNewLine & "===Row 2 Column 1" & VbNewLine & "===Row 2 Column 2" & VbNewLine & VbNewLine & "Hello World"

input = Server.HtmlEncode(input)

' Links
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(https?://\S+[^\s@,.""']+)", "<a target=""_blank"" href=""$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a target=""_blank"" href=""http://$1"">$1</a>")

' Email Addresses
input = RegularExpressions.Regex.Replace(input, "(?<!\S)\w[\w\.]*\w?@[\w\.]+\w", "<a href=""mailto:$0"">$0</a>")

' Spaces
input = RegularExpressions.Regex.Replace(input, "  ", " &nbsp;")

' Line Breaks
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")

' Dash Symbol
input = RegularExpressions.Regex.Replace(input,"-- ", "&mdash; ")

' Bullet Symbol
input = RegularExpressions.Regex.Replace(input, "\{\-\}", "&#8226;")

' Copyright Symbol
input = RegularExpressions.Regex.Replace(input, "\{[cC]\}", "&#169;")

' Trademark Symbol
input = RegularExpressions.Regex.Replace(input, "\{[tT][mM]\}", "&#8482;")

' Registered Trademark Symbol
input = RegularExpressions.Regex.Replace(input, "\{[rR]\}", "&#174;")

' Fix Double Entities
input = RegularExpressions.Regex.Replace(input, "(?<=&)amp;(?=#?\w+;)", String.Empty)



Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.