当前位置:网站首页>[QT notes] basic use of qregularexpression in QT

[QT notes] basic use of qregularexpression in QT

2022-06-27 06:09:00 Lin Qi sevenlin

  • QT5 The use of regular expressions in... Is recommended QRegularExpression, No longer use QRegExp
  • QRegularExpression in comparison with QRegExp Optimized and improved

The main classes used

  • QRegularExpression Create regular expression objects
  • QRegularExpressionMatch Get string matching results
  • QRegularExpressionMatchIterator Get string match result iterator ( The global matching , Match multiple )

The main function

  • verification
  • String search
  • String find and replace
  • String segmentation
Create objects ( structure )
QRegularExpression re("a pattern");
Set regular expression mode
QRegularExpression re;
re.setPattern("another pattern");
Get regular expression pattern
QRegularExpression re("a third pattern");
QString pattern = re.pattern(); // pattern == "a third pattern"
Set mode options
//  Case insensitive 
QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);

//  Multi-line matching 
QRegularExpression re("^\\d+$");
re.setPatternOptions(QRegularExpression::MultilineOption);
Get mode options
QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption | 
QRegularExpression::DotMatchesEverythingOption);

QRegularExpression::PatternOptions options = re.patternOptions();
// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
string matching ( Match result single )

The capture group in the pattern is from 1 Numbered starting , Implicitly capture group number 0 Used to capture substrings that match the entire pattern .

//  Check whether the match is successful 
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("abc123 def");
bool hasMatch = match.hasMatch(); // true

//  Get string matching results 
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("abc123 def");
if (match.hasMatch()) {
    QString matched = match.captured(0); // matched == "23 def"
}

//  From the offset 1 Start matching string 
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
if (match.hasMatch()) {
    QString matched = match.captured(0); // matched == "45 def"
}

//  Extract string   Use captured(int nth = 0)
QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
QRegularExpressionMatch match = re.match("08/12/1985");
if (match.hasMatch()) {
    QString day = match.captured(1); // day == "08"
    QString month = match.captured(2); // month == "12"
    QString year = match.captured(3); // year == "1985"
}

//  Extract string , Use captured(const QString &name)
QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
QRegularExpressionMatch match = re.match("08/12/1985");
if (match.hasMatch()) {
    QString date = match.captured("date"); // date == "08"
    QString month = match.captured("month"); // month == "12"
    QString year = match.captured("year"); // year == 1985
}
Get substring index
QRegularExpression re("abc(\\d+)def");
QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
if (match.hasMatch()) {
    //  Starting index 
    int startOffset = match.capturedStart(1); // startOffset == 6
    //  End index 
    int endOffset = match.capturedEnd(1); // endOffset == 9
}
The global matching ( Multiple matching results )
QRegularExpression re("(\\w+)");
QRegularExpressionMatchIterator i = re.globalMatch("the quick fox")
QStringList words;
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    QString word = match.captured(1);
    words << word;
}  // words contains "the", "quick", "fox"
Check whether the regular expression has syntax errors
QRegularExpression invalidRe("(unmatched|parenthesis");
if (!invalidRe.isValid()) {
    QString errorString = invalidRe.errorString(); // errorString == "missing )"
    int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
}
Partial matching

Apply to

  • User input validation
  • Incremental multisegment matching

Match type

  • QRegularExpression::PartialPreferCompleteMatch
    The pattern string partially matches the topic string . If a partial match is found , Record it , And try other matching alternatives as usual . If an exact match is found , Takes precedence over partial matching ; under these circumstances , Only complete matches are reported . contrary , If no exact match is found ( But only a partial match ), Then the report partially matches .
  • QRegularExpression::PartialPreferFirstMatch
    The pattern string partially matches the topic string . If a partial match is found , Then the match stops and a partial match is reported . under these circumstances , Will not try other matching alternatives ( May result in an exact match ). Besides , This matching type assumes that the topic string is just a substring of larger text , also ( In this text ) There are other characters besides the end of the topic string , This can lead to surprising results .
/***************QRegularExpression::PartialPreferCompleteMatch***************/
//  Example 1
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
QRegularExpression re(pattern);
QString input("Jan 21,");
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
//  The whole match 
bool hasMatch = match.hasMatch(); // false
//  Partial matching 
bool hasPartialMatch = match.hasPartialMatch(); // true
QString captured = match.captured(0); // captured == "Jan 21,"

//  Example 2
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
QRegularExpression re(pattern);
QString input("Dec 8, 1985");
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
bool hasMatch = match.hasMatch(); // true
bool hasPartialMatch = match.hasPartialMatch(); // false

//  Example 3
QRegularExpression re("abc\\w+X|defY");
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
QString captured = match.captured(0); // captured == "abcdef"


/***************QRegularExpression::PartialPreferFirstMatch***************/
//  Example 1
QRegularExpression re("abc|ab");
QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
  
//  Example 2
QRegularExpression re("abc(def)?");
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true

//  Example 3
QRegularExpression re("(abc)*");
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
bool hasMatch = match.hasMatch(); // false
bool hasPartialMatch = match.hasPartialMatch(); // true
Regular expression basic syntax

https://blog.csdn.net/lin786063594/article/details/125301089?spm=1001.2014.3001.5501

Learning materials

QT Help document

原网站

版权声明
本文为[Lin Qi sevenlin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270546235564.html