查看: 95|回复: 0

正则表达式匹配双引号常用例子总结

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-12-3
发表于 2024-5-14 09:29:32 | 显示全部楼层 |阅读模式

1.正则表达式匹配引号

匹配双引号"\"匹配单引号''

2.正则表达式匹配正则表达式中用到的特殊符号时需加\\

()[]{}/|\-+匹配[\\[匹配]\\]匹配\\\\\匹配/\\/匹配|\\|匹配-\\-匹配+\\+

匹配大写英文或小写英文或数字或下划线用\\w0-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;

总结 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部