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

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();
}

输出结果: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();
}

检测关键字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");
...

输出结果16

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

\d是匹配数字

4.{count}

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

输出结果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();
}

输出结果为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();
}

输出结果为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();

匹配成功,返回0:

string str = "";
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();

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();
}

{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();

正则表达式入门(c#)的更多相关文章

  1. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  2. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

  3. 分享录制的正则表达式入门、高阶以及使用 .NET 实现网络爬虫视频教程

    我发布的「正则表达式入门以及高阶教程」,欢迎学习. 课程简介 正则表达式是软件开发必须掌握的一门语言,掌握后才能很好地理解到它的威力: 课程采用概念和实验操作 4/6 分隔,帮助大家理解概念后再使用大 ...

  4. JavaScript正则表达式详解(一)正则表达式入门

    JavaScript正则表达式是很多JavaScript开发人员比较头疼的事情,也很多人不愿意学习,只是必要的时候上网查一下就可以啦~本文中详细的把JavaScript正则表达式的用法进行了列表,希望 ...

  5. 转载 Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  6. 转载 Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写.转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式 ...

  7. php正则表达式入门-常用语法格式

    php正则表达式入门-常用语法格式 原文地址:http://www.jbxue.com/article/24467.html 分享下php正则表达式中的一些常用语法格式,用于匹配字母.数字等,个人感觉 ...

  8. 正则表达式入门之学习路线&七个问题

    由于工作需求,需要使用正则表达式查找满足某种模式的字符串,但因为之前都没有接触过相关内容,最开始的时候看了一些已经被别人写好了的正则表达式,本来打算可能可以直接使用: 最全的常用正则表达式大全——包括 ...

  9. python正则表达式入门篇

    文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...

  10. Java正则表达式入门基础篇

    正则表达式是一种可以用于模式匹配和替换的规范,一个正则表达式就是由普通的字符(例如字符a到z)以及特殊字符(元字符)组成的文字模式,它 用以描述在查找文字主体时待匹配的一个或多个字符串.正则表达式作为 ...

随机推荐

  1. POJ_3342_Party_at_Hali-Bula

    #include <iostream> #include <map> #include <cstring> using namespace std; int Gra ...

  2. Struts2(三)更改字符编码

    一.导入包和struts配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE stru ...

  3. 浅析MySQL各种索引

    MySQL各种索引(由于是浅析大多都不刻意区分搜索引擎) INDEX(普通索引):最主要的索引.没有不论什么限制 ALTER TABLE `table_name` ADD INDEX index_na ...

  4. exception javax.crypto.BadPaddingException: Given final block not properly padded

      exception javax.crypto.BadPaddingException: Given final block not properly padded CreationTime--20 ...

  5. UrlConnection的代理和返回状态码的问题

    今天写了一段代码想在service里访问一个外部网站,在service的方法里写了如下代码 System.setProperty("http.proxyType", "4 ...

  6. 以__name__进行单元测试

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #以__name__进行单元测试 #何为单元测试? #单元测试(模块测试)是开发者编写的一小段代码,用于检测被 ...

  7. Android利用温度传感器实现带动画效果的电子温度计

    概述 Android利用温度传感器实现带动画效果的电子温度计. 详细 代码下载:http://www.demodashi.com/demo/10631.html 一.准备工作 需要准备一部带有温度传感 ...

  8. Xcode编译 No such file or directory

    No such file or directory 差点儿相同算是Xcode比較常见的一个编译错误了.原因往往是加入或删除美术资源的时候出错.尽管是小问题,但出现的频率非常高. 解决方法(能够依次尝试 ...

  9. Javakeyword之this

    this的作用: 1) this是当前对象的一个引用.便于对当前对象參数的使用. 2)能够返回对象的自己这个类的引用.同一时候还能够在一个构造函数其中调用还有一个构造函数 this演示样例: publ ...

  10. 【mysql】Innodb三大特性之adaptive hash index

    1.Adaptive Hash Indexes 定义 If a table fits almost entirely in main memory, the fastest way to perfor ...