对于C11中的正則表達式的使用
Regular Expression Special Characters
"."---Any single character(a "wildcard")
"["---Begin character class
"]"---End character class
"{"---Begin count
"}"---End count
"("---Begin grouping
")"---End grouping
"\"---Next character has a special meaning
"*"---Zero or more
"+"---One or more
"?"---Optional(zero or one)
"!"---Alternative(or)
"^"---Start of line; negation
"$"---End of line
Example:
case 1:
^A*B+C?
$
explain 1:
以A开头。有多个或者没有B。有至少一个C。之后有没有都能够,结束。
A pattern can be optional or repeated(the default is exactly once) by adding a suffix:
Repetition
{n}---Exactly n times;
{n,}---no less than n times;
{n,m}---at least n times and at most m times;
*---Zero or more , that is , {0,}
+---One or more, that is ,{1,}
?---Optional(zero or one), that is {0,1}
Example:
case 1:
A{3}B{2,4}C*
explain 1:
AAABBC or AAABBB
A suffix ? after any of the repetition notations makes the pattern matcher "lazy" or "non-greedy".
That is , when looking for a pattern, it will look for the shortest match rather than the lonest.
By default, the pattern matcher always looks for the longest match (similar to C++'s Max rule).
Consider:
ababab
The pattern (ab)*matches all of "ababab". However, (ab)*? matches only the first "ab".
The most common character classifications have names:
Character Classes
alnum --- Any alphanumeric character
alpha --- Any alphanumeric character
blank --- Any whitespace character that is not a line separator
cntrl --- Any control character
d --- Any decimal digit
digit --- Any decimal digit
graph --- Any graphical character
lower --- Any lowercase character
print --- Any printable character
punct --- Any punctuation character
s --- Any whitespace character
space --- Any whitespace character
upper --- Any uppercase charater
w --- Any word character(alphnumeric characters plus the underscore)
xdigit --- Any hexadecimal digit character
Several character classes are supported by shorthand notation:
Character Class Abbreviations
\d --- A decimal digit --- [[:digit:]]
\s --- A space (space tab,...) --- [[:space:]]
\w --- A letter(a-z) or digit(0-9) or underscore(_) --- [_[:alnum:]]
\D --- Not \d --- [^[:digit:]]
\S --- Not \s --- [^[:space:]]
\W --- Not \w --- [^_[:alnum:]]
In addition, languages supporting regular expressions often provide:
Nonstandard (but Common) Character Class Abbreviations
\l --- A lowercase character --- [[:lower:]]
\u --- An uppercase character --- [[:upper;]]
\L --- Not \l --- [^[:lower:]]
\U --- Not \u --- [^[:upper:]]
Note the doubling of the backslash to include a backslash in an ordinary string literal.
As usual, backslashes can denote special charaters:
Special Characters
\n --- Newline
\t --- Tab
\\ --- One backslash
\xhh -- Unicode characters expressed using twp hexadecimal digits
\uhhh --- Unicode characters expressed using four hexadecimal digits
To add to the opportunites for confusion, two further logically differents uses of the backslash are provided:
Special Characters
\b --- The first or last character of a word (a "boundary character")
\B --- Not a \b
\i --- The ith sub_match in this pattern
Here are some examples of patterns:
Ax* //A,Ax,Axxxx
Ax+ //Ax,Axxx not A
\d-?
\d //1-2,12 not 1--2
\w{2}-d{4,5} //Ab-1234,XX54321,22-5432
(\d*:)?(\d+) //12:3, 1:23, 123, :123 Not 123:
(bs|BS) //bs ,BS Not bS
[aeiouy] //a,o,u An English vowel, not x
[^aeiouy] //x,k Not an English vowel, not e
[a^eiouy] //a,^,o,u An Engish vowel or ^
以下是測试代码:
#include <iostream>
#include <regex> using namespace std; int main()
{
const char* reg_esp = "^A*B+C? $";
regex rgx(reg_esp);
cmatch match;
const char* target = "AAAAAAAAABBBBBBBBC";
if(regex_search(target,match,rgx))
{
for(size_t a = 0;a < match.size();a++)
cout << string(match[a].first,match[a].second) << endl;
}
else
cout << "No Match Case !" << endl;
return 0;
}
对于C11中的正則表達式的使用的更多相关文章
- hive中使用正則表達式不当导致执行奇慢无比
业务保障部有一个需求,须要用hive实时计算上一小时的数据.比方如今是12点,我须要计算11点的数据,并且必须在1小时之后执行出来.可是他们用hive实现的时候发现就单个map任务执行都超过了1小时, ...
- C++ Tr1中的正則表達式
要使用正則表達式,首先要有类库支持,C++曾经不像Java或者C#有完整的类库使用,可是在Tr1中早已提供了正则库,仅仅是非常少被人们注意罢了 TR1中包括了一个正则库,来自Boost的 regex, ...
- javascript中的正則表達式
对文本数据进行操作是JavaScript中常见的任务.正則表達式通过同意程序猿指定字符串匹配的模式来简化诸如验证表单中输入是否具有正确格式之类的任务. 1.正則表達式标记: 字符 含义 举例 i 大写 ...
- vim中使用正則表達式
一.使用正則表達式的命令 使用正則表達式的命令最常见的就是 / (搜索)命令. 其格式例如以下: /正則表達式 还有一个非常实用的命令就是 :s(替换)命令,将第一个//之间的正則表達式替换成第二个/ ...
- C++11中正則表達式測试
VC++2010已经支持regex了, 能够用来编译下述代码. #include <string> #include <regex> #include <iostream ...
- JAVA中正則表達式总结
昨天,我的朋友请教我正則表達式.我也好久没有写过正則表達式了,昨天刚好看了下如鹏网创始人杨中科老师关于正則表達式的解说.使我加深了正則表達式的印像.现我把他总结下: 很多语言,包含Perl.PHP.P ...
- JAVA中正則表達式总结(具体解释及用途)
很多语言,包含Perl.PHP.Python.JavaScript和JScript,都支持用正則表達式处理文本,一些文本编辑器用正則表達式实现高级"搜索-替换"功能.所以JAVA语 ...
- python使用正則表達式
python中使用正則表達式 1. 匹配字符 正則表達式中的元字符有 . ^ $ * + ? { } [ ] \ | ( ) 匹配字符用的模式有 \d 匹配随意数字 \D 匹配随意非 ...
- python 学习笔记 10 -- 正則表達式
零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...
随机推荐
- SecureCRT学习之道:SecureCRT 常用技巧
快捷键: 1. ctrl + a : 移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...
- poj 2992 Divisors 整数分解
设m=C(n,k)=n!/((n-k)!*k!) 问题:求m的因数的个数 将m分解质因数得到 p1有a1个 p2有a2个 .... 因为每一个质因数能够取0~ai个(所有取0就是1,所有取ai就是m) ...
- 神经网络BP算法C和python代码
上面只显示代码. 详BP原理和神经网络的相关知识,请参阅:神经网络和反向传播算法推导 首先是前向传播的计算: 输入: 首先为正整数 n.m.p.t,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...
- windows phone (14) 简单了解Ellipse元素和Rectangle元素
原文:windows phone (14) 简单了解Ellipse元素和Rectangle元素 System.Windows.Shapes命名空间中包含了显示矢量图形的元素分别为ellipse和re ...
- python学习笔记之十:文件和素材
这里将介绍函数和对象--文件和流,让你在程序调用期间存储数据,并且可以处理来自其他程序的数据. 一. 打开文件 1.1 open函数 open函数用来打开文件,语法如下:open(name,[.mod ...
- 足球和oracle系列(3):oracle过程排名,世界杯第二回合战罢到来!
足球与oracle系列(3):oracle进程排名.世界杯次回合即将战罢! 声明: 这不是技术文档,既然学来几招oracle简单招式.就忍不了在人前卖弄几下.纯为茶余饭后与数朋库友的插科 ...
- 【原创】java中的父进程子进程 —— 坑爹的java Runtime.getRuntime().exec
最近有一个需求,需要用一个java进程启动多个子进程来完成并发任务.由于必须给用户完成任务的反馈,所以需要父进程记录子进程的生命周期. exec方法返回一个Process对象,在当前进程内调用该对象的 ...
- 【读书札记】建立第一个Web项目
安装配置好jdk.tomcat,我用的版本号是7.0.54,我放在C:\server\apache-tomcat-7.0.54下, CATALINA_BASE:C:\server\apache-tom ...
- cocos2d-x3.0rc 版 设置模拟器窗体大小
由于刚接触这职业时间不是非常长.也是第一次写博客,假设有错误的地方还请大神们指出,开通这博客目的非常easy相互学习和讨论(更重要的是记录工作中学到的东西以方便以后自己查阅) 先后參与过两个项目.只是 ...
- 一个简单的带缓存http代理
眼下1.0版模型非常easy.即对客户机发来的请求进行简单处理后,转发到server.转发之前先检查本地缓存.假设有.则直接回送给客户本地资源 程序流程大致例如以下图: 缓存是通过把文件保存到磁盘上, ...