我想要加密HTML文档的文本内容而不更改其布局。内容存储在成对的标记中,如下所示:< span style ...> text_to_get </span>。我的想法是使用正则表达式来检索(1)并用加密文本(2)替换每个文本部分。我完成了步骤(1),但在步骤(2)中遇到了麻烦。这里是代码我工作:使用正则表达式替换HTML标记内容
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一个简单的解决方案。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – David 2012-03-19 17:24:18