正则表达式入门(c#)

本文是对该教程的学习练习

http://www.jb51.net/tools/zhengze.html

 

注:正则符号转义和普通的转义一样,加反斜杠,比如[ 变成 \[

 

正则表达式符号和转义符号最好用+号连起来。

关于在vs里用正则表达式替换需要分组,其实很简单用括号括起来就算一个组了

匹配到的数据都在Groups里面,C#提供了匹配,替换等功能,具体msdn

 

1.\bContent\b

复制代码
static void Main(string[] args)
{
    string str = "Act game - Uncharted3, act Game - God of war";
    Regex rex = new Regex(@"\bact\b");
    var result = rex.Match(str);
    if (result.Success)
    {
        var tmp = result.Index;
        Console.WriteLine(tmp);
    }
    else
    {
        Console.WriteLine("failure");
    }
    Console.Read();
}
View Code
复制代码

输出结果:23。

第一个act,a是大写。没做大小写匹配,所以正则匹配到的是索引23的那个act.

前面的\b是匹配开始处,后面的是匹配结束处。开始处和结束处指空格

比如只匹配开始处就是\bContent

 

 

2.\bContent1\b.*\bContent2\b

 

复制代码
static void Main(string[] args)
{
    string str = "rpg game - Legend of Heroes, act game - Uncharted3, act Game - God of war";
    Regex rex = new Regex(@"\bact\b.*\bUncharted3\b");
    var result = rex.Match(str);
    if (result.Success)
    {
        var tmp = result.Index;
        Console.WriteLine(tmp);
    }
    else
    {
        Console.WriteLine("failure");
    }
    Console.Read();
}
View Code
复制代码

 

检测关键字1后面是否跟着关键字2

输出结果:29。

但是遇到多个和前缀相同的字串,就会出问题。

如果不是查找单词,可以去掉\b。Content1.*Content2

 

3.0\d\d-\d\d\d\d\d\d\d\d

string str = "the xxxx, xxxx, 021-88888888";
Regex rex = new Regex(@"0\d\d-\d\d\d\d\d\d\d\d");
...
View Code

输出结果16

算是占位符,匹配电话号码啥的。代码后面都一样就省略掉。

\d是匹配数字

 

4.{count}

 

string str = "the xxxx, xxxx, 021-88888888";
Regex rex = new Regex(@"0\d{2}-\d{8}");
...
View Code

 

输出结果16

上面那种写法的优化版。

 

5.{min,max}

 

\d是数字,\d{x}是匹配长度为x的数字,\d{x1,x2}是匹配长度最小为x1,最大为x2的数字

复制代码
static void Main(string[] args)
        {
            string str = "Act game - Uncharted3, act Game - God of war qwe  123456   asd";
            Regex rex = new Regex(@"\d{5,12}");
            var result = rex.Match(str);
            if (result.Success)
            {
                var tmp = result.Index;
                Console.WriteLine(tmp);
            }
            else
            {
                Console.WriteLine("failure");
            }
            Console.Read();
        }
View Code
复制代码

 

输出结果为50

 

6.\s和\w

\s是匹配空格,花括号内容同上

复制代码
static void Main(string[] args)
        {
            string str = "Act game - Uncharted3, act Game - God of war qwe     asd";
            Regex rex = new Regex(@"\s{5,12}");
            var result = rex.Match(str);
            if (result.Success)
            {
                var tmp = result.Index;
                Console.WriteLine(tmp);
            }
            else
            {
                Console.WriteLine("failure");
            }
            Console.Read();
        }
View Code
复制代码

输出结果为48

 

\w是匹配字母或数字或下划线或汉字。和\s差不多,不做示范了。

 

7.^和$

 

用于匹配整个字符串,通常注册帐号的时候,非常有用

失败,因为是整串匹配:

复制代码
string str = "QQ: 123456";
Regex rex = new Regex(@"^\d{5,12}$");
var result = rex.Match(str);
if (result.Success)
{
    var tmp = result.Index;
    Console.WriteLine(tmp);
}
else
{
    Console.WriteLine("failure");
}
Console.Read();
View Code
复制代码

匹配成功,返回0:

复制代码
string str = "123456";
Regex rex = new Regex(@"^\d{5,12}$");
var result = rex.Match(str);
if (result.Success)
{
    var tmp = result.Index;
    Console.WriteLine(tmp);
}
else
{
    Console.WriteLine("failure");
}
Console.Read();
View Code
复制代码

 

8. * + ? {n,}

*    重复零次或更多次

+   重复一次或更多次

?    重复零次或一次

{n,}  重复n次或更多次

 

*和?测试的时候返回的总是0。暂时先搁一边

 

+,测试正常,返回4

复制代码
static void Main(string[] args)
        {
            string str = "QQ: 111a";
            Regex rex = new Regex(@"\d+\w");
            var result = rex.Match(str);
            if (result.Success)
            {
                var tmp = result.Index;
                Console.WriteLine(tmp);
            }
            else
            {
                Console.WriteLine("failure");
            }
            Console.Read();
        }
View Code
复制代码

 

{n,} 这里设置数字重复2次或者更多,返回值为4

复制代码
string str = "QQ: 111a";
Regex rex = new Regex(@"\d{2,}\w");
var result = rex.Match(str);
if (result.Success)
{
    var tmp = result.Index;
    Console.WriteLine(tmp);
}
else
{
    Console.WriteLine("failure");
}
Console.Read();
View Code
复制代码

 

posted @   HONT  阅读(353)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示
回到顶部