c#使用正则表达式要用到System.Text.RegularExprssions命名空间

官方API

Regex类是用于匹配表达式:

  通常Regex分为静态类和实例化俩种方式。那这俩种有什么区别呢,一般情况下使用静态的Regex的时候,.net会自动将正则表达式缓存起来,而在使用相同的时候正则的时候不会重新编译。一般情况下会缓存15个正则。而实例化一次Regex,则会重新编译正则。

Match代表获取的正则结果:

  Match.value:代表获取正则内容

  Match.Index:代表内容的其实下标

  Match.Length:代表内容长度

  Match.Groups:代表正则所有捕获组,默认Group[0]为全部捕获内容,如果同一个捕获组有多个内容,则取最后一个

  Match.Captures如果一个捕获组有多个内容,Group代表最后一个,而Captures代表这个捕获组的全部内容  

  MatchCollection 一个可迭代的正则集合。

  Match.Success:判断是否匹配

  Match.Next:如果匹配到了多个结果,返回下一个

  Match.Result:替换模式,$1能够匹配到捕获组内容,对$1进行修饰如“--$1--”,则将匹配到的捕获组内容替换为--()--

public class Example
{
public static void Main()
{
string pattern = "--(.+?)--";
string replacement = "($1)";
string input = "He said--decisively--that the time--whatever time it was--had come.";
foreach (Match match in Regex.Matches(input, pattern))
{
string result = match.Result(replacement);
Console.WriteLine(result);
}
}
}
// The example displays the following output:
// (decisively)
// (whatever time it was)

Group:表示单个捕获组的结果正则中使用。而在正则中使用?:可以避免捕获组。

Capture:表示单个成功子表达式捕获的结果。

using System;
using System.Text.RegularExpressions; public class Test
{ public static void Main ()
{ // Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase); // Define a test string.
string text = "The the quick brown fox fox jumps over the lazy dog dog."; // Find matches.
MatchCollection matches = rx.Matches(text); // Report the number of matches found.
Console.WriteLine("{0} matches found in:\n {1}",
matches.Count,
text); // Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[].Index,
groups[].Index);
} } }
// The example produces the following output to the console:
// 3 matches found in:
// The the quick brown fox fox jumps over the lazy dog dog.
// 'The' repeated at positions 0 and 4
// 'fox' repeated at positions 20 and 25
// 'dog' repeated at positions 50 and 54

C#正则的使用的更多相关文章

  1. Javascript正则对象方法与字符串正则方法总结

    正则对象 var reg = new Regexp('abc','gi') var reg = /abc/ig 正则方法 test方法(测试某个字符串是否匹配) var str = 'abc123'; ...

  2. C#-正则,常用几种数据解析-端午快乐

    在等待几个小时就是端午节了,这里预祝各位节日快乐. 这里分享的是几个在C#中常用的正则解析数据写法,其实就是Regex类,至于正则的匹配格式,请仔细阅读正则的api文档,此处不具体说明,谢谢. 开始吧 ...

  3. Javascript 中 with 的替代方案和String 中的正则方法

    这几天在升级自己的MVVM 框架,遇到很多小问题,就在这里统一解决了. with 语法 在代码中,要执行这么一个函数 function computeExpression(exp, scope) { ...

  4. JavaScript与PHP中正则

    一.JavaScript 有个在线调试正则的工具,点击查看工具.下面的所有示例代码,都可以在codepen上查看到. 1.创建正则表达式 var re = /ab+c/; //方式一 正则表达式字面量 ...

  5. Java正则速成秘籍(一)之招式篇

    导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...

  6. Java正则速成秘籍(二)之心法篇

    导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...

  7. Java正则速成秘籍(三)之见招拆招篇

    导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...

  8. python浅谈正则的常用方法

    python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...

  9. [Python基础知识]正则

    import re str4 = r"^http://qy.chinahr.com/cvm/preview\?cvid=\w{24,25}&from=sou&gtid=\w{ ...

  10. iOS中使用正则

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

随机推荐

  1. 当有多个相同的DIV时,我怎么判断我点击的是哪个

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 清除windows 远程桌面访问记录 批处理

    直接上码供参考 @echo OFF color 0a Title Clear Windows Recent Mode con cols=109 lines=30 :START ECHO. Echo = ...

  3. C++ 一周刷完C++基础课程(同C程序进行比较)

    **参考bilibili视频av29504365** ### 一段简单的程序Hello World```#include <iostream>using namespace std;int ...

  4. selenium-java爬虫实现

    推荐的网站学习网站 1.官方文档 http://www.seleniumhq.org/docs/ 2.selenium多线程 http://www.cnblogs.com/dingmy/p/34380 ...

  5. spring+mybatis事务配置(转载)

    原文地址:http://blog.csdn.net/wgh1015398431/article/details/52861048 申明式事务配置步骤 .xml文件头部需要添加spring的相关支持: ...

  6. vue interceptors(拦截器)

    拦截器 顾名思义: 就是半路个您劫持, 拦截器 其实在项目和自己写demo中,总会遇到请求方面需要在请求头里面做判断或者添加一些东西, 这时候 vue 中应用中axios的 interceptors  ...

  7. Python-Django的windows环境

    下载安装python2.7 : 最好是安装win32的,64bit的很多的lib都不支持.python-2.7.3 http://python.org/getit/releases/2.7.3/ 下载 ...

  8. 错误:The selected wizard could not be started Plug-in com.genuitec.eclipse.j2ee.ui was unable to load class com.genuitec.eclipse.j2ee.ui.wizard.WebProjectWizard

    错误:The selected wizard could not be started Plug-in com.genuitec.eclipse.j2ee.ui was unable to load ...

  9. 【NOIP2014模拟11.3】蛋糕

    题目 今天是Bessie的生日,他买了一个蛋糕和朋友们一起分享,蛋糕可以看成是一个R行C列的表格,共有R*C个格子,每个格子都有一个0至9的数字,表示该格子蛋糕拥有的巧克力.现在Bessie要把蛋糕横 ...

  10. spark数据倾斜与解决方法

    一.数据倾斜 数据倾斜一般发生在对数据进行重新划分以及聚合的处理过程中.执行Spark作业时,数据倾斜一般发生在shuffle过程中,因为Spark的shuffle过程需要进行数据的重新划分处理.在执 ...