Qt使用正则表达式

Qt中使用正则表达式的类是QRegExp

头文件 <QRegExp>

先定义正则规则

正则规则跟其他的语言一样样

QRegExp rx("^[a-zA-Z].+$") 

rx.indexIn(QString("fwlfwiefwf wefwefwef")) 

返回的是匹配对的第一个下标位置 如果没有匹配的话返回的是-1

所以只需要判断返回值是不是-1就可以确定了

官方例子

QString str = "offsets: 1.23 .50 71.00 6.00";
QRegExp rx("\\d*\\.\\d+");    // primitive floating point matching
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
    ++count;
    pos += rx.matchedLength();
}
// pos will be 9, 14, 18 and finally 24; count will end up as 4

官方文档

https://doc.qt.io/qt-5/qregexp.html#indexIn

发布了239 篇原创文章 · 获赞 31 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/soulwyb/article/details/100008740