Asp.net 如何处理<;或>;在C#2.0中使用正则表达式的字符串类型
下面是用C#2.0编写的正则表达式,用于删除不需要的querystring(excludeList中存在的任何内容)的代码将从页面querystring中排除,它对我来说运行良好Asp.net 如何处理<;或>;在C#2.0中使用正则表达式的字符串类型,asp.net,regex,c#-2.0,Asp.net,Regex,C# 2.0,下面是用C#2.0编写的正则表达式,用于删除不需要的querystring(excludeList中存在的任何内容)的代码将从页面querystring中排除,它对我来说运行良好 string querystring = string.Empty; string excludeList = "cid,incid,h"; querystring = Regex.Replace(Regex.Replace(
string querystring = string.Empty;
string excludeList = "cid,incid,h";
querystring = Regex.Replace(Regex.Replace(Regex.Replace(HttpContext.Current.Request.Url.Query, @"^\?", "&"), "&(" + excludeList.Replace(",", "|") + ")=[^&]*", "", RegexOptions.IgnoreCase), "^&", "?");
现在我想修改我的常规表达式,这样,如果我的excludeList包含如下内容,那么如果我的页面querystring中有<或>,我将进行编码
string excludeList = "cid,incid,h,<,>";
现在一切正常,我想用上面的正则表达式对“”进行编码。试试这个
(?is)^(?<del>[^\?]+?)(?<retain>\?.+)$
(?is)^(?[^\?]+)(?\?。+)$
解释
@"
(?is) # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^ # Assert position at the beginning of the string
(?<del> # Match the regular expression below and capture its match into backreference with name “del”
[^\?] # Match any character that is NOT a ? character
+? # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?<retain> # Match the regular expression below and capture its match into backreference with name “retain”
\? # Match the character “?” literally
. # Match any single character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
@”
(?is)#将正则表达式的其余部分与选项匹配:不区分大小写(i);点匹配换行符(s)
^#在字符串开头断言位置
(?#匹配下面的正则表达式,并将其匹配捕获到名为“del”的backreference中
[^\?]#匹配任何非?字符的字符
+?#在一次和无限次之间,尽可能少的次数,根据需要扩展(懒惰)
)
(?#匹配下面的正则表达式,并将其匹配捕获到名为“retain”的backreference中
\?按字面意思匹配字符“?”
.#匹配任何单个字符
+#在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)
$#在字符串末尾(或字符串末尾的换行符之前,如果有)断言位置
"
更新代码
string resultString = null;
try {
resultString = Regex.Replace(subjectString, @"(?is)^(?<del>[^?]+?)(?<retain>\?.+)$", "${retain}");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
string resultString=null;
试一试{
resultString=Regex.Replace(subjectString,@“(?is)^(?[^?]+?)(?\?。+)$”,“${retain}”);
}捕获(参数异常){
//正则表达式中的语法错误
}
试试这个
(?is)^(?<del>[^\?]+?)(?<retain>\?.+)$
(?is)^(?[^\?]+)(?\?。+)$
解释
@"
(?is) # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^ # Assert position at the beginning of the string
(?<del> # Match the regular expression below and capture its match into backreference with name “del”
[^\?] # Match any character that is NOT a ? character
+? # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?<retain> # Match the regular expression below and capture its match into backreference with name “retain”
\? # Match the character “?” literally
. # Match any single character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
@”
(?is)#将正则表达式的其余部分与选项匹配:不区分大小写(i);点匹配换行符(s)
^#在字符串开头断言位置
(?#匹配下面的正则表达式,并将其匹配捕获到名为“del”的backreference中
[^\?]#匹配任何非?字符的字符
+?#在一次和无限次之间,尽可能少的次数,根据需要扩展(懒惰)
)
(?#匹配下面的正则表达式,并将其匹配捕获到名为“retain”的backreference中
\?按字面意思匹配字符“?”
.#匹配任何单个字符
+#在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)
$#在字符串末尾(或字符串末尾的换行符之前,如果有)断言位置
"
更新代码
string resultString = null;
try {
resultString = Regex.Replace(subjectString, @"(?is)^(?<del>[^?]+?)(?<retain>\?.+)$", "${retain}");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
string resultString=null;
试一试{
resultString=Regex.Replace(subjectString,@“(?is)^(?[^?]+?)(?\?。+)$”,“${retain}”);
}捕获(参数异常){
//正则表达式中的语法错误
}
@如何在我现有的正则表达式中使用上述正则表达式,我想在我现有的正则表达式中添加,请建议如何处理