正则表达式

  • 正则表达式即一个文本匹配字符串的一种模式,Qt中QRegExp类实现使用正则表达式进行模式匹配,且完全支持Unicode,主要应用:字符串验证、搜索、查找替换、分割。

  • 正则表达式中字符及字符集

  • 正则表达式中的量词

  • 正则表达式中的断言

  • QRegExp同时支持通配符

  • 新建控制台应用程序,项目名QRegExp
    编辑main.cpp文件

    //匹配字符
        QRegExp ret("a");
        qDebug() << "是否匹配成功: " << ret.exactMatch("abc");    //false
        qDebug() << "是否匹配成功: " << ret.exactMatch("a");      //true
    
    //匹配数字
        QRegExp retNum("(\\d*\\D{2})");
        qDebug() << "是否匹配成功: " << retNum.exactMatch("120kg");    //true
        qDebug() << "是否匹配成功: " << retNum.exactMatch("180cm");    //true
        qDebug() << "是否匹配成功: " << retNum.exactMatch("3M");       //false
    
    //匹配通配符
        QRegExp re("*.txt") ;
        re.setPatternSyntax(QRegExp::Wildcard); //支持通配符
        qDebug() << "通配符匹配: " << re.exactMatch("a.txt");         //true
        qDebug() << "通配符匹配: " << re.exactMatch("a.txt.bak");     //false
    
    //匹配单词边界
        QRegExp reg("\\b(hello|Hello)\\b");
        qDebug() << "多个单词匹配: " << reg.indexIn("helloEveryone");     //-1表示失败
        qDebug() << "多个单词匹配: " << reg.indexIn("Hmm Hello Everyone");//4 表示匹配到的字符位置
    
    //匹配捕获的文本
    //以 "(?:" 开始, 以 ")" 结束
        QRegExp regHeight("(\\d+)(?:\\s*)(cm|inch)");
        int res = regHeight.indexIn("zhangsan 170cm");
        if(res > -1){
            qDebug() << "regHeight(0):" << regHeight.cap(0);    //170cm
            qDebug() << "regHeight(1):" << regHeight.cap(1);    //170
            qDebug() << "regHeight(2):" << regHeight.cap(2);    //cm
        }
    
    //断言 (?!) 表示:不紧跟才能匹配
        QRegExp regAssertFalse("面(?!包)");
        QString str = "面包没了,只剩面条了";
        str.replace(regAssertFalse, "龙虾鲍鱼");
        qDebug() << str;    //输出: "面包没了,只剩龙虾鲍鱼了"
    	
    //断言 (?=) 表示:紧跟才匹配
        QRegExp regAssertTrue("面(?=包)");
        QString str1 = "面包没了,只剩面条了";
        str1.replace(regAssertTrue, "草");
        qDebug() << str1;    //输出: "龙虾鲍鱼没了,只剩面条了"
    

qt5新类

 //Qt5新类
    QRegularExpression regExp("hello");
    qDebug() << "QRegularExpression匹配字符: " <<regExp.match("hello world"); //输出(0, 5, "hello")

    regExp.setPattern("([A-Z]{3,8})");
    regExp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);    //大小写不敏感

    qDebug() << "大小写不敏感匹配字符: " <<regExp.match("hello");


    //新类捕获匹配
    QRegularExpression regExp1("^(\\d\\d\\d\\d)/(\\d\\d)/(\\d\\d)$");
    QRegularExpressionMatch match0 = regExp1.match("2022/04/04");


    if(match0.isValid()){
        QString strMatch0 = match0.captured(0);
        QString year = match0.captured(1);
        QString month = match0.captured(2);
        QString day = match0.captured(3);

        qDebug() << "日期: " << strMatch0;
        qDebug() << "年: " << year;
        qDebug() << "月: " << month;
        qDebug() << "日: " << day;
    }


    QString sPattern;
    sPattern = "^(Jan|Feb|Mar|Apr|May) \\d\\d \\d\\d\\d\\d$";
    QRegularExpression regDate(sPattern);

    QString ss("Feb 02 2022");
    QRegularExpressionMatch dateMatch;
    dateMatch = regDate.match(ss, 0,
                              QRegularExpression::PartialPreferCompleteMatch); //部分匹配
    bool b_HasMatched = dateMatch.hasMatch();        //完整匹配的返回值
    bool b_Partial = dateMatch.hasPartialMatch();    //部分匹配的返回值

    qDebug() << b_HasMatched;
    qDebug() << b_Partial;

更多推荐

Qt正则表达式