RegEx replace of html codes to ascii codes

Discussion in 'ASP .Net' started by Greg --, Aug 9, 2005.

  1. Greg  --

    Greg -- Guest

    Is it possible to replace html codes, such as $#63;, to the ASCII
    equivalent with RegEx?

    Using RegEx, I'm able to obtain the 63, but I'm unable to convert it:

    Regex.Replace("?", "&#(.+?);", "$1") 'Replaces with 63
    Regex.Replace("?", "&#(.+?);", Chr("$1")) 'Replaces with garbage
    instead of ?
    Greg --, Aug 9, 2005
    #1
    1. Advertising

  2. Greg  --

    Oliver Wong Guest

    "Greg --" <> wrote in message
    news:...
    > Is it possible to replace html codes, such as $#63;, to the ASCII
    > equivalent with RegEx?
    >
    > Using RegEx, I'm able to obtain the 63, but I'm unable to convert it:
    >
    > Regex.Replace("?", "&#(.+?);", "$1") 'Replaces with 63
    > Regex.Replace("?", "&#(.+?);", Chr("$1")) 'Replaces with garbage
    > instead of ?
    >


    Doesn't the Chr function take an integer (or perhaps a byte) and not a
    string? Might have to parse the string into an integer before passing it to
    Chr.

    - Oliver
    Oliver Wong, Aug 9, 2005
    #2
    1. Advertising

  3. Greg  --

    Greg Guest

    Yeah, but I was using late-binding.

    I have the same problem specifying it as:
    Regex.Replace("?", "&#(.+?);", Chr(CInt("$1")))
    Greg, Aug 9, 2005
    #3
  4. Greg  --

    tom pester Guest


    > I have the same problem specifying it as:
    > Regex.Replace("?", "&#(.+?);", Chr(CInt("$1")))


    You are using an expression as the 3th argument. The _result_ of this expression
    will have $1 substituted by the first group.
    So $1 is just $1 as the argument of Chr which gives an error.

    You need to use a Matchevaluator (a function that gets called for each match)
    : http://msdn.microsoft.com/library/d...regularexpressionsregexclassreplacetopic1.asp


    Why don't you just use Server.HTMLdecode?

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <script runat="server">

    string ToASCII(Match m)
    {
    string match = m.Groups[1].ToString();
    char ASCII = (char)Convert.ToInt32(match);
    return ASCII.ToString();
    }


    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <%
    Response.Write("<BR>Regex solution<BR>");
    Response.Write(Regex.Replace("ab?cd", "&#(.+?);", new
    MatchEvaluator(ToASCII)));
    Response.Write("<BR>Server.HtmlDecode solution<BR>");
    Response.Write(Server.HtmlDecode("ab?cd"));
    %>
    </div>
    </form>
    </body>
    </html>

    Let me know if you have any more questions...

    Cheers,
    Tom Peste
    tom pester, Aug 9, 2005
    #4
  5. Greg  --

    Greg Guest

    Thanks works, thanks.
    Greg, Aug 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TOXiC
    Replies:
    5
    Views:
    1,218
    TOXiC
    Jan 31, 2007
  2. Replies:
    2
    Views:
    2,794
    Malcolm
    Aug 20, 2005
  3. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
  4. James O'Brien
    Replies:
    3
    Views:
    234
    Ben Morrow
    Mar 5, 2004
  5. Alextophi
    Replies:
    8
    Views:
    482
    Alan J. Flavell
    Dec 30, 2005
Loading...

Share This Page