当前位置:首页 > 开发 > C# > 正文内容

C# 正则表达式例子

C#5个月前 (05-21)

单独匹配

            string text = "订单号: 12345, 数量: 8, 价格: 99";
            MatchCollection matches = Regex.Matches(text, @"\d+");

            foreach (Match m in matches)
            {
                Console.WriteLine(m.Value);
            }
            // 输出:
            // 12345
            // 8
            // 99.5




带子成员

            string lrcLine = "[无尽的华尔兹1][无尽的华尔兹2]";
            MatchCollection matches = Regex.Matches(lrcLine, @"\[(.*?)\]");
            foreach (Match m in matches)
            {
                Console.WriteLine("成员");
                Console.WriteLine($"{m.Groups[0].Value}");
                Console.WriteLine($"子成员");
                Console.WriteLine($"{m.Groups[1].Value}");
                Console.WriteLine(  "--------");
            }
            // 输出:
            //成员
            //[无尽的华尔兹1]
            //子成员
            //无尽的华尔兹1
            //--------
            //成员
            //[无尽的华尔兹2]
            //子成员
            //无尽的华尔兹2
            //--------





转载请注明出处。

本文链接:http://www.pythonopen.com/?id=7950

相关文章

C# 延时命令

方法1System.Threading.Thread.Sleep(1000);缺点:如果在主线程使用...

C# MDI例子

父窗口属性IsMdiContainer设置为Trueprivate void b...

C# XML

创建    XmlDocument xmlDoc ...

C# 跳出foreach循环

在 C# 中,如果你想在 foreach 循环内部提前跳出当前这一轮循环,继续执行下一轮循环,可以使...

C# BackgroundWorker,在DoWork里更新控件内容

一般情况下不可以直接在BackgroundWorker的DoWork事件中更新 UI 控件在Back...

C# Control防闪烁

SetStyle(ControlStyles.AllPaintingInWmPaint |...