【发布时间】:2012-04-04 03:53:34
【问题描述】:
我想加密 HTML 文档的文本内容而不更改其布局。内容以成对的标签存储,如下所示:text_to_get。我的想法是使用正则表达式来检索 (1) 并用加密文本 (2) 替换每个文本部分。我完成了第 (1) 步,但在第 (2) 步遇到了麻烦。这是我正在处理的代码:
private string encryptSpanContent(string text, string passPhrase, string salt, string hash, int iteration, string initialVector, int keySize)
{
string resultText = text;
string pattern = "<span style=(?<style>.*?)>(?<content>.*?)</span>";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(resultText);
foreach (Match match in matches)
{
string replaceWith = "<span style=" + match.Groups["style"] + ">" + AESEncryption.Encrypt(match.Groups["content"].Value, passPhrase, salt, hash, iteration, initialVector, keySize) + "</span>";
resultText = regex.Replace(resultText, replaceWith);
}
return resultText;
}
这是错误的行吗(使所有文本都被最后一个 replaceWith 值替换)?
resultText = regex.Replace(resultText, replaceWith);
谁能帮我解决这个问题?
【问题讨论】:
-
不要使用正则表达式解析 HTML。 stackoverflow.com/questions/1732348/…
标签: c# .net html regex replace