1.正则表达式匹配引号
匹配双引号"
用\"
匹配单引号'
用'
2.正则表达式匹配正则表达式中用到的特殊符号时需加\\
如()[]{}/|\-+
匹配[
用\\[
匹配]
用\\]
匹配\
用\\\\
匹配/
用\\/
匹配|
用\\|
匹配-
用\\-
匹配+
用\\+
匹配大写英文或小写英文或数字或下划线用\\w
或0-9a-za-z_
3.正则表达式中各种扩号()[]{}作用
中括号[]
表示匹配单个字符,匹配中扩号里列出的任意一个字符
[dsa]//匹配d或s或a
小括号()
表示匹配字符串,匹配小扩号里列出的所有字符构成的字符串
(dsaff) //仅能匹配dsaff
大括号{}
表示匹配的次数,放于()或[]
之后
[dsa]{1,8}//匹配1-8次[dsa],如匹配d,dd,dddddddd
(dsa){1,8}//匹配1-8次(dsa),如匹配dsa,dsadsadsadsadsadsadsadsa
4.常用匹配例子
例子(匹配英语键盘上的任意非空字符)
qregexp re("^[\\w~!@#$%^&*()+`={}:;<>?,.|'\"\[\\]\\-\\/\\\\]+$"); qstring test("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]{}|;:'\"\\/,.<>?"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的有理数)
qregexp reg("^(\\-(?!0(?!\\.))|\\+(?!0(?!\\.)))?(0|[1-9]\\d*)(\\.\\d+)?$"); qstring test("41424.4155346"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的非负有理数)
qregexp reg("^(\\+(?!0(?!\\.)))?(0|[1-9]\\d*)(\\.\\d+)?$"); qstring test("41424.4155346"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的正有理数)
qregexp reg("^(\\+)?(0(?=\\.)|[1-9]\\d*)(\\.\\d+)?$"); qstring test("41424.4155346"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的整型数字)
qregexp reg("^(\\-(?!0)|\\+(?!0))?(0|[1-9]\\d*)$"); qstring test("414246"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的非负整型数字)
qregexp reg("^(\\+(?!0))?(0|[1-9]\\d*)$"); qstring test("414246"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的正整型数字)
qregexp reg("^(\\+)?([1-9]\\d*)$"); qstring test("414246"); bool match = re.exactmatch(test); //match=true
例子(匹配任意合法表示的密码)
qregexp reg("^[\\w~!@#$%^&*()+`={}:;<>?,.|'\"\[\\]\\-\\/\\\\]+$"); if (!reg.exactmatch(value.data())) { message_ = qobject::tr("the password can only contanin numbers, english " "characters or special characters ") .tostdstring(); return false; } return true;
总结
到此这篇关于正则表达式匹配双引号常用例子的文章就介绍到这了,更多相关正则表达式匹配双引号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论