RegEx replace of html codes to ascii codes

G

Greg --

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 ?
 
O

Oliver Wong

Greg -- said:
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
 
G

Greg

Yeah, but I was using late-binding.

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

tom pester

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top