`

常用正则表达式(整理中...)

    博客分类:
  • C#
阅读更多

1 提取中括号[]中内容

            string input = "asdf[111]sdfsf[222]sdfsdf";
            Regex r = new Regex(@"\[([^\]]*)\]");
            MatchCollection match = r.Matches(input);
            foreach (Match m in match)
            {
                Console.WriteLine(m.Groups[0].Value);
                Console.WriteLine(m.Groups[1].Value);
            }

 

2 替换中括号[]中内容,包括[]也替换

        string s = Regex.Replace(input, @"\[([^\]]*)\]", new MatchEvaluator(myEvaluator));

        private string myEvaluator(Match m)
        {
            string s1 = m.Groups[0].Value;
            string s2 = m.Groups[1].Value;

            return "_" + s2 + "_";
        }

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics