什么是正则表达式
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
简单的来说正则表达式就是特殊的字符序列,用于描述要匹配的文本模式。
现在假设你需要从200M的邮件里寻找一个与一个地址相近的格式,那么使用正则表达式便可以快速完成。
正则表达式的基本语法
1、Literals 文字
输入什么就匹配什么。
目标字符串:dog
正则表达式:dog
2、Alternation 选择
|
左右两边表示选择关系,相当于或。
目标字符串:
cat
dog
正则表达式:dog|cat
3、Character Sets 字符集
[gab]
表示在可以匹配方括号里的任何一个字符。
[^gab]
开头加上^
表示除了方括号内以外的字符。
目标字符串:
cat
hat
rat
正则表达式:[chr]at
4、Wildcards 通配符
.
是通配符,表示匹配任何一个字符。
\.
是.
的转义字符。
目标字符串:
bear.
lion.
orca.
正则表达式:....[\.]
5、Ranges 范围
[c-e]
表示一个从c到e的字母
[0-9]
表示任何一个数字
注:[A-Za-z]
这种表示方法也是合理的,表示所有字母(包括大写和小写)。
目标字符串:
cub
dog
elk
正则表达式:[c-e][uol][bgk]
6、Shorthand Character Classes 速记字符
介绍6种速记字符。
\w
:相当于[A-Za-z0-9_]
,表示所有字母与数字。
\d
:相当于[0-9]
,表示数字。
\s
:相当于[ \t\r\n\f\v]
,表示制表符、回车符、换行符、分页符、垂直制表符。
\W
:相当于[^A-Za-z0-9_]
,除\w
以外的字符。
\D
:相当于[^0-9]
,表示除\d
以外的字符。
\S
:相当于[^ \t\r\n\f\v]
,表示除\s
以外的字符。
目标字符串:
5 sloths
8 llamas
7 hyenas.
正则表达式:\d\s\w\w\w\w\w\w
7、Grouping 分组
()内的正则表达式是一组的,具体看例子。
目标字符串:
puppies are my favorite!
kitty cats are my favorite!
正则表达式:(puppies|kitty cats) are my favorite!
8、Quantifiers-Fixed 固定的量词
用一个花括号里面有一个数字的形式来代表重复,比如:
a{3}
:表示aaa
b{3,5}
:表示bbb
、bbbb
、bbbbb
注意:量词是贪心的,它会匹配最长的字符串。比如ab{3,4}
会匹配abbbb
字符串中的abbbb
而不是abbb
目标字符串:
squeaaak
squeaaaak
squeaaaaak
正则表达式:squea{3,5}k
9、Quantifiers - Optional 可选择的量词
?
表示这个字符是可选的。
as?
既可以匹配as
,也可以匹配a
。
目标字符串:
1 duck for adoption?
5 ducks for adoption?
7 ducks for adoption?
正则表达式:\d ducks? for adoption\?
10、Quantifiers - 0 or More, 1 or More 0或更多/1或更多的量词
*
表示前面的字符出现0或多次。
+
表示前面的字符出现1或多次。
目标字符串:
hoot
hoooooot
hooooooooooot
不能匹配的字符串:
hot
正则表达式:hoo+t
11、Quantifiers - Optional 可选择的量词
?
表示这个字符是可选的。
as?
既可以匹配as
,也可以匹配a
。
目标字符串:
1 duck for adoption?
5 ducks for adoption?
7 ducks for adoption?
正则表达式:\d ducks? for adoption\?
12、Anchors 锚点
^hat
表示必须匹配以hat
开头的字符。
tail$
表示必须匹配以tail
结尾的字符。
目标字符串:
penguins are cooler than regular expressions
不能匹配的字符串:
king penguins are cooler than regular expressions
penguins are cooler than regular expressions!
正则表达式:^penguins are cooler than regular expressions$?
在C++中使用正则表达式
regex_match()
#include<cstdio>
#include<regex>
#include<iostream>
#include<string>
using namespace std;
int main(){
if(regex_match("hooooo!",regex("ho+!"))){
cout<<"match! 1"<<endl;
}
string s = "hoooooo!ho!hoo!";
regex expr("(ho+!)(ho+!)(ho+!)");
if(regex_match(s,expr)){
cout<<"match! 2"<<endl;
}
if(regex_match(s.begin(),s.end(),expr)){
cout<<"match! 3"<<endl;
}
smatch sm;
regex_match (s,sm,expr);
cout<<sm.size()<<endl;
for (unsigned i=0; i<sm.size(); ++i) {
cout << "[" << sm[i] << "] ";
}
cout << endl;
}
regex_search()
#include<cstdio>
#include<regex>
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hohoha";
regex regexp("\\w{1,4}");
smatch m;
regex_search(s,m,regexp);
for(auto x:m){
cout<<x<<endl;
}
}
regex_replace()
#include<cstdio>
#include<regex>
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hohoha";
regex regexp("\\w{1,4}");
cout<< regex_replace(s,regexp,"ok")<<endl;
}