string str = "abcd123"; string str1 = "ab{"; string result = Regex.Replace(str, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; }); string result1 = Regex.Replace(str1, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; }); Response.Write(result + "<br/>"); Response.Write(result1 + "<br/>"); /* abcd ab{ */
string s = "abcd123"; string result = ""; Match ma = Regex.Match(s, @"(&#\d+)"); if (ma.Groups.Count == 1) { result = Regex.Replace(s, @"\d+", ""); } MessageBox.Show(result);
string str = "abcd123";
string str1 = "ab{";
string result = Regex.Replace(str, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; });
string result1 = Regex.Replace(str1, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; });
Response.Write(result + "<br/>");
Response.Write(result1 + "<br/>");
/*
abcd
ab{
*/
string s = "abcd123";
string result = "";
Match ma = Regex.Match(s, @"(&#\d+)");
if (ma.Groups.Count == 1)
{
result = Regex.Replace(s, @"\d+", "");
}
MessageBox.Show(result);
我用正则表达式“(?<!&#)(\d+)\b”
问题如下:
"abcd123" 没有问题,结果是abc
"ab{" 出现问题,结果是ab 想要的结果是ab{
if("ab{".StartWith("&#"))
{
....
}