Qt正則表達式使用舉例
正則表達式
正則表達式即一個文本匹配字符串的一種模式,Qt中QRegExp類實現(xiàn)使用正則表達式進行模式匹配,且完全支持Unicode,主要應(yīng)用:字符串驗證、搜索、查找替換、分割。
正則表達式中字符及字符集
正則表達式中的量詞
正則表達式中的斷言
QRegExp同時支持通配符
新建控制臺應(yīng)用程序,項目名QRegExp
編輯main.cpp文件//匹配字符 QRegExp ret("a"); qDebug() << "是否匹配成功: " << ret.exactMatch("abc"); //false qDebug() << "是否匹配成功: " << ret.exactMatch("a"); //true //匹配數(shù)字 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 表示匹配到的字符位置 //匹配捕獲的文本 //以 "(?:" 開始, 以 ")" 結(jié)束 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;
附正則表達式提取字符串中的數(shù)字方法 :
QString WndName("WndName1"); QRegExp rx("\\d+"); QString wndIndex; rx.indexIn(WndName,0); wndIndex=rx.cap(1); qDebug()<<wndIndex;
正則表達式從一個字符串里提取特定的字段或數(shù)據(jù) 例如從"a=100"里提取"a"和"100" :
QString pattern("(.*)=(.*)"); QRegExp rx(pattern); QString str("a=100"); int pos = str.indexOf(rx); // 0, 第一個的位置 // 如果找不到str,則返回-1 // 你也可以使用rx.indexin(str) qDebug() << pos; if ( pos >= 0 ) { qDebug() << rx.matchedLength(); // 5, 匹配字符串到最后的長度 // 或者-1如果沒有匹配 qDebug() << rx.capturedTexts(); // QStringList("a=100", "a", "100"), // 0: 文本匹配模式 // 1: 第一個()捕獲的文本 // 2: 第二個文本的捕獲 qDebug() << rx.cap(0); // a=100, 文本匹配模式 qDebug() << rx.cap(1); // a, 第n個()捕獲的文本 qDebug() << rx.cap(2); // 100, qDebug() << rx.pos(0); // 0, 第n個捕獲文本的位置 qDebug() << rx.pos(1); // 0 qDebug() << rx.pos(2); // 2 }
總結(jié)
到此這篇關(guān)于Qt正則表達式使用的文章就介紹到這了,更多相關(guān)Qt正則表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu)之Treap詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之Treap詳解,本文講解了Treap的基本知識、Treap的基本操作、Treap的高級操作技巧等,需要的朋友可以參考下2014-08-08C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法
這篇文章主要介紹了C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法,涉及C++數(shù)組的遍歷、判斷、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03