QRegexp、QRegularExpression的用法学习
QRegexp
- Regexp 由表达式(expressions) 量词(quantifiers) 断言(assertions)
int QRegExp::indexIn() //匹配成功返回索引 失败 -1
setPattern() //输入一个RegExp
replace() //匹配替换
“^” 在“[]”中开始的位置 表达相反的意思 如[^abc] 匹配除abc以外的字符
“-” 表示一个范围的字符 [A-Z];
-
E 代表一个表达式 一个表达式可以是一个字符,或者每一个字符集缩写或者 方括号中的一个字符集,或者括号中的一个表达式
-
量词一般是”贪婪的“(尽可能的去匹配多个) setMinimal (true) 设置为”非贪婪”
-
E{n,m} : "n"表示匹配至少匹配 n次 "m"表示至多匹配 m次
-
断言 不匹配任何字符
QRegExp re("^\\d\\d?$"); // ^字符串开头 $字符串结尾 字面意思 匹配规则 两个纯数字
qDebug()<<re.indexIn("1c");
qDebug()<<re.indexIn("9");
qDebug()<<re.indexIn("c2");
qDebug()<<re.indexIn("23");
qDebug()<<re.indexIn("256");
qDebug()<<"******************";
re.setPattern("\\b(mail|letter)\\b"); // \b匹配字符串边界 \转义字符
qDebug()<<re.indexIn("emailletter");
qDebug()<<re.indexIn("my mail");
qDebug()<<re.indexIn("my email letter");
qDebug()<<"******************";
re.setPattern("Q(?!t)");
QString str = "Hi I'm Q";
str.replace(re,"Qt");
qDebug()<<"str:"<<str;
QString str2="I used Q,your used Q too,his used Qt";
str2.replace(re,"Qt");
qDebug()<<"str2:"<<str2;
qDebug()<<"******************";
QString str3="One Eric another Eirik, and an Ericsson"
"How many Eiriks,Eric?";
QRegExp re2("\\bEi?ri[ck]\\b"); // 匹配E ric Ei rik
int pos =0;
int count = 0;
while (pos >=0)
{
pos = re2.indexIn(str3,pos);
if(pos >=0)
{
++pos;
++count;
}
}
qDebug()<<"count:"<<count;
-
setPatternSyntax() //切换不同的匹配方式
-
-
通配符 一种比QRegExp 更简单的匹配方式
-
//通配符
qDebug()<<"********************";
QRegExp re3("*.txt");
re3.setPatternSyntax(QRegExp::Wildcard); //通配符
qDebug()<<re3.exactMatch("readme.txt");
qDebug()<<re3.exactMatch("welcome.txt.bat");
-
cap() 0 全匹配 1左边第一个括号的匹配结果 捕获匹配结果
qDebug()<<"********************";
//文本捕获
QRegExp re4("(\\d+)");
QString str4="Offsets:12 14 99 256 99";
QStringList list;
int pos2 =0;
while((pos2=re4.indexIn(str4,pos2)) !=-1)
{
list<<re4.cap(1); //捕获到的第一个文本
pos2 +=re4.matchedLength(); //上一个匹配到的字符串的长度
}
qDebug()<<list;
QRegExp relen("(\\d+)(?:\\s*)(cm|inch)");
int pos3=relen.indexIn("Length: 189cm");
if(pos3>-1)
{
QString value =relen.cap(1);
QString unit = relen.cap(2);
QString string = relen.cap(0);
qDebug()<<value<<unit<<string;
}
QRegExp re5("\\b(\\w+)\\W+\\1\\b");
re5.setCaseSensitivity(Qt::CaseInsensitive); //不区分大小写
qDebug()<<re5.indexIn("Hello--hello");
qDebug()<<re5.cap(0);
qDebug()<<re5.cap(1);
QRegExp re6("\\b你好\\b");
qDebug()<<re6.indexIn("你好");
qDebug()<<re6.cap(0); //整个字符串完全匹配 使用cap(0) 捕获
QValidator 验证器
QRegularExpression
模式选项
//方式一
QRegularExpression re("Qt rocks",QRegularExpression::CaseInsensitiveOption);
//方式二
QRegularExpression re("^\\d+$")
re.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpression::PatternOptions options =re.patternOptions() //获取模式设置
//QRegularExpression
qDebug()<<"******************";
QRegularExpression regexp("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
QRegularExpressionMatch match = regexp.match("28/10/2020");
if(match.hasMatch())
{
QString matched = match.captured(0);
QString day = match.captured(1);
QString month = match.captured(2);
QString year = match.captured(3);
qDebug()<<"matched:"<<matched;
qDebug()<<"day:"<<day;
qDebug()<<"month :"<<month;
qDebug()<<"year :"<<year;
}
QString pattern("^(Jan|Feb|Mar|APr|May)\\s\\d\\d?,\\d\\d\\d\\d$");
QRegularExpression rel(pattern);
QString input("Jan 21,2020");
QRegularExpressionMatch match1 = rel.match(input,0,QRegularExpression::PartialPreferCompleteMatch);
bool hasMatch = match1.hasMatch(); //完全匹配
bool hasPartialMath = match1.hasPartialMatch(); //返回部分匹配结果
qDebug()<<"部分匹配的全部结果"<<match1.captured(0);
qDebug()<<"hasMatch:"<<hasMatch<<"hasPartialMath:"<<hasPartialMath;
QRegularExpression ret("\\d\\d\\w+");
QRegularExpressionMatch match2=ret.match("12 abd 56 def",1); //设置偏移为1 会跳过第一个匹配到的数据