[英]Replace an existing url in given text with a new url using Regex
我正在尝试使用正则表达式将给定文本中的现有网址替换为新网址。 我似乎没有与我使用的正则表达式匹配:
string regex = "<a href=\"http://domain/page.asp?id=(\\d+)&oid=(\\d+)&type=(\\w+)\">";
有人可以帮我写一个正确的模式来找到类似于以下内容的网址:
"<A href=\"http://domain/page.asp?id=38957&oid=2497&type=JPG\">"
以下是我的测试代码,找不到与我使用的模式匹配的任何代码:
string result = string.Empty;
string sampleText = "<A href=\"http://domain/page.asp?id=38957&oid=2497&type=JPG\"><U>Click here for Terms & Conditions...</U></A>";
string regex = "<a href=\"http://domain/page.asp?id=(\\d+)&oid=(\\d+)&type=(\\w+)\">";
Regex regEx = new Regex(regex, RegexOptions.IgnoreCase);
result= regEx.Replace(text, "<a href=\"/newPage/Index/$1&opid=$2)\">");
除了一切看起来都很好.
和?
是正则表达式中的特殊字符,因此需要转义以将其视为文字。 所以你的表情:
string regex = "<a href=\"http://domain/page.asp?id=(\\d+)&oid=(\\d+)&type=(\\w+)\">";
需要是:
string regex = "<a href=\"http://domain/page\\.asp\\?id=(\\d+)&oid=(\\d+)&type=(\\w+)\">";
请注意前面的反斜杠.
和?
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.