Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

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>
<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>
</span>
Hello Again World

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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]);
        output.Append(strFormatted);
    }
    else if (line.Count() > 0)
    {
        output.Append(line);
   }
    else
    {
        output.Append("</span>\r\n");
        ended = true; 
    }
}

Console.WriteLine(output.ToString());

Open in new window

Avatar of hankknight

ASKER

I want this to be done using REGEX and I need to to work with VB, not C#.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

I'd have to see your full code. What I posted above is working code.

User generated image
Make sure you have the Imports statement at the top of your code file.
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)

Response.write(input)

%>

Open in new window