正则表达式入门及备忘
2015-07-14 20:39
274 查看
概述
正则表达式,主要是用符号描述了一类特定的文本(模式)。而正则表达式引擎则负责在给定的字符串中,查找到这一特定的文本。本文主要是列出常用的正则表达式符号,加以归类说明。本文仅仅是快速理解了正则表达式相关元字符,作一个备忘,供以后理解更复杂表达式的参考,以后关于正则表达式的相关内容会持续更新本文。示例语言用C#
概述
普通字符
字符集合
速记的字符集合
指定重复次数的字符
匹配位置字符
分支替换字符
匹配特殊字符
组,反向引用,非捕获组
贪婪与非贪婪
回溯与非回溯
正向预搜索、反向预搜索
最后
1 普通字符
最简单的一种文本描述,就是直接给出要匹配内容。 如要在”Generic specialization, the decorator pattern, chains of responsibilities, and extensible software.” 找到pattern,那么正则式就直接是”heels”即可string input1 = "hello 1024 world 8080 bye"; Regex reg1 = new Regex(@"\d{4}(?= world)"); if (reg1.IsMatch(input1)) { Console.WriteLine(reg1.Match(input1).Value);//output 1024 } Regex reg2 = new Regex(@"\d{4}(?! world)"); if (reg2.IsMatch(input1)) { Console.WriteLine(reg2.Match(input1).Value);//output 8080 } Regex reg3 = new Regex(@"(?<=world )\d{4}"); if (reg3.IsMatch(input1)) { Console.WriteLine(reg3.Match(input1).Value);//output 8080 } Regex reg4 = new Regex(@"(?<!world )\d{4}"); if (reg4.IsMatch(input1)) { Console.WriteLine(reg4.Match(input1).Value);//output 1024 }
View Code
最后
参考地址:正则表达式30分钟入门教程
Regular Expressions Tutorial
NET Framework Regular Expressions
.NET进阶系列之一:C#正则表达式整理备忘
相关文章推荐
- SG函数的详细解释
- 第一周编程作业 温度转换
- 第一天
- 检查变量的类型
- 安卓编程问题1
- background-attachment 网页背景固定
- Hash散列算法 Time33算法
- 2016阿里巴巴校招offer面经
- /var/log目录
- java字符串常量池知识
- 暑假选拔赛02 ---- 可行方案
- Source Insight 常用设置和快捷键大全
- Linux中 ps aux 命令
- 抽象工厂模式
- 欢迎使用CSDN-markdown编辑器
- [转]C++之运算符重载(2)
- 字符串的全排列
- 【C++】深度探索C++对象模型读书笔记--构造函数语义学(The Semantics of constructors)(一)
- DB2 V10的nosql DB 介绍 和命令接口 未完待续---
- 南阳理工102 次方求模