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

C# 正则表达式例子

C#1周前 (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://pythonopen.com/?id=7950

返回列表

上一篇:C# Lambda表达式例子

没有最新的文章了...

相关文章

C# 时间操作

获取系统已运行时间    System.Environment.Tic...

C# 可空参数

using System; using System.Runtime.Inte...

C# BackgroundWorker的例子

以下是一个使用 BackgroundWorker 组件在 C# 中实现后台执行任务,同时在主线程更新...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...

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

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

C# OnMeasureItem

1. **整体功能概述**   - `OnMeasureItem` 是一个在Wi...