Improve / fix QRegularExpression* docs
Fixed a couple of typos; also, wrapped the snippets in a main() function, so that now the snippet file can be compiled (and therefore the compiler can help us at detecting those typos). Change-Id: Ie182a9c4cb451db13a6f4bfa5eaed66bc6966c8f Reviewed-by: Topi Reiniö <topi.reinio@digia.com> Reviewed-by: Casper van Donderen <casper.vandonderen@gmail.com> Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
This commit is contained in:
parent
589a18597f
commit
a2eff5c244
@ -38,17 +38,28 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QStringList>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QRegularExpressionMatch>
|
||||||
|
#include <QRegularExpressionMatchIterator>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
{
|
||||||
//! [0]
|
//! [0]
|
||||||
QRegularExpression re("a pattern");
|
QRegularExpression re("a pattern");
|
||||||
//! [0]
|
//! [0]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [1]
|
//! [1]
|
||||||
QRegularExpression re;
|
QRegularExpression re;
|
||||||
re.setPattern("another pattern");
|
re.setPattern("another pattern");
|
||||||
//! [1]
|
//! [1]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [2]
|
//! [2]
|
||||||
// matches two digits followed by a space and a word
|
// matches two digits followed by a space and a word
|
||||||
QRegularExpression re("\\d\\d \\w+");
|
QRegularExpression re("\\d\\d \\w+");
|
||||||
@ -56,27 +67,31 @@ QRegularExpression re("\\d\\d \\w+");
|
|||||||
// matches a backslash
|
// matches a backslash
|
||||||
QRegularExpression re2("\\\\");
|
QRegularExpression re2("\\\\");
|
||||||
//! [2]
|
//! [2]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [3]
|
//! [3]
|
||||||
QRegularExpression re("a third pattern");
|
QRegularExpression re("a third pattern");
|
||||||
QString pattern = re.pattern(); // pattern == "a third pattern"
|
QString pattern = re.pattern(); // pattern == "a third pattern"
|
||||||
//! [3]
|
//! [3]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [4]
|
//! [4]
|
||||||
// matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
|
// matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
|
||||||
QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
|
QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
|
||||||
//! [4]
|
//! [4]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [5]
|
//! [5]
|
||||||
QRegularExpression re("^\\d+$");
|
QRegularExpression re("^\\d+$");
|
||||||
re.setPatternOptions(QRegularExpression::MultilineOption);
|
re.setPatternOptions(QRegularExpression::MultilineOption);
|
||||||
// re matches any line in the subject string that contains only digits (but at least one)
|
// re matches any line in the subject string that contains only digits (but at least one)
|
||||||
//! [5]
|
//! [5]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [6]
|
//! [6]
|
||||||
QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
|
QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
|
||||||
| QRegularExpression::DotMatchesEverythingOption);
|
| QRegularExpression::DotMatchesEverythingOption);
|
||||||
@ -84,16 +99,18 @@ QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::M
|
|||||||
QRegularExpression::PatternOptions options = re.patternOptions();
|
QRegularExpression::PatternOptions options = re.patternOptions();
|
||||||
// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
|
// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
|
||||||
//! [6]
|
//! [6]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [7]
|
//! [7]
|
||||||
// match two digits followed by a space and a word
|
// match two digits followed by a space and a word
|
||||||
QRegularExpression re("\\d\\d \\w+");
|
QRegularExpression re("\\d\\d \\w+");
|
||||||
QRegularExpressionMatch match = re.match("abc123 def");
|
QRegularExpressionMatch match = re.match("abc123 def");
|
||||||
bool hasMatch = match.hasMatch(); // true
|
bool hasMatch = match.hasMatch(); // true
|
||||||
//! [7]
|
//! [7]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [8]
|
//! [8]
|
||||||
QRegularExpression re("\\d\\d \\w+");
|
QRegularExpression re("\\d\\d \\w+");
|
||||||
QRegularExpressionMatch match = re.match("abc123 def");
|
QRegularExpressionMatch match = re.match("abc123 def");
|
||||||
@ -102,8 +119,9 @@ if (match.hasMatch()) {
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [8]
|
//! [8]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [9]
|
//! [9]
|
||||||
QRegularExpression re("\\d\\d \\w+");
|
QRegularExpression re("\\d\\d \\w+");
|
||||||
QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
|
QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
|
||||||
@ -112,31 +130,34 @@ if (match.hasMatch()) {
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [9]
|
//! [9]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [10]
|
//! [10]
|
||||||
QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
|
QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
|
||||||
QRegularExpressionMatch match = re.match("08/12/1985");
|
QRegularExpressionMatch match = re.match("08/12/1985");
|
||||||
if (match.hasMatch()) {
|
if (match.hasMatch()) {
|
||||||
QString day = re.captured(1); // day == "08"
|
QString day = match.captured(1); // day == "08"
|
||||||
QString month = re.captured(2); // month == "12"
|
QString month = match.captured(2); // month == "12"
|
||||||
QString year = re.captured(3); // year == "1985"
|
QString year = match.captured(3); // year == "1985"
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [10]
|
//! [10]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [11]
|
//! [11]
|
||||||
QRegularExpression re("abc(\\d+)def");
|
QRegularExpression re("abc(\\d+)def");
|
||||||
QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
|
QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
|
||||||
if (match.hasMatch()) {
|
if (match.hasMatch()) {
|
||||||
int startOffset = re.capturedStart(1); // startOffset == 6
|
int startOffset = match.capturedStart(1); // startOffset == 6
|
||||||
int endOffset = re.capturedEnd(1); // endOffset == 9
|
int endOffset = match.capturedEnd(1); // endOffset == 9
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [11]
|
//! [11]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [12]
|
//! [12]
|
||||||
QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
|
QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
|
||||||
QRegularExpressionMatch match = re.match("08/12/1985");
|
QRegularExpressionMatch match = re.match("08/12/1985");
|
||||||
@ -146,14 +167,14 @@ if (match.hasMatch()) {
|
|||||||
QString year = match.captured("year"); // year == 1985
|
QString year = match.captured("year"); // year == 1985
|
||||||
}
|
}
|
||||||
//! [12]
|
//! [12]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [13]
|
//! [13]
|
||||||
QRegularExpression re("(\\w+)");
|
QRegularExpression re("(\\w+)");
|
||||||
QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
|
QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
|
||||||
//! [13]
|
//! [13]
|
||||||
|
|
||||||
|
|
||||||
//! [14]
|
//! [14]
|
||||||
QStringList words;
|
QStringList words;
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
@ -163,72 +184,86 @@ while (i.hasNext()) {
|
|||||||
}
|
}
|
||||||
// words contains "the", "quick", "fox"
|
// words contains "the", "quick", "fox"
|
||||||
//! [14]
|
//! [14]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [15]
|
//! [15]
|
||||||
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
|
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
|
||||||
QRegularExpression re(pattern);
|
QRegularExpression re(pattern);
|
||||||
|
|
||||||
QString input("Jan 21,");
|
QString input("Jan 21,");
|
||||||
QRegularExpressionMatch match = re.match(input, 0, QRegularExpressionMatch::PartialPreferCompleteMatch);
|
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
|
||||||
bool hasMatch = match.hasMatch(); // false
|
bool hasMatch = match.hasMatch(); // false
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // true
|
bool hasPartialMatch = match.hasPartialMatch(); // true
|
||||||
//! [15]
|
//! [15]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
|
||||||
|
QRegularExpression re(pattern);
|
||||||
//! [16]
|
//! [16]
|
||||||
QString input("Dec 8, 1985");
|
QString input("Dec 8, 1985");
|
||||||
QRegularExpressionMatch match = re.match(input, 0, QRegularExpressionMatch::PartialPreferCompleteMatch);
|
QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
|
||||||
bool hasMatch = match.hasMatch(); // true
|
bool hasMatch = match.hasMatch(); // true
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // false
|
bool hasPartialMatch = match.hasPartialMatch(); // false
|
||||||
//! [16]
|
//! [16]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [17]
|
//! [17]
|
||||||
QRegularExpression re("abc\\w+X|def");
|
QRegularExpression re("abc\\w+X|def");
|
||||||
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpressionMatch::PartialPreferCompleteMatch);
|
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
|
||||||
bool hasMatch = match.hasMatch(); // true
|
bool hasMatch = match.hasMatch(); // true
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // false
|
bool hasPartialMatch = match.hasPartialMatch(); // false
|
||||||
QString captured = match.captured(0); // captured == "def"
|
QString captured = match.captured(0); // captured == "def"
|
||||||
//! [17]
|
//! [17]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [18]
|
//! [18]
|
||||||
QRegularExpression re("abc\\w+X|defY");
|
QRegularExpression re("abc\\w+X|defY");
|
||||||
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpressionMatch::PartialPreferCompleteMatch);
|
QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
|
||||||
bool hasMatch = match.hasMatch(); // false
|
bool hasMatch = match.hasMatch(); // false
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // true
|
bool hasPartialMatch = match.hasPartialMatch(); // true
|
||||||
QString captured = match.captured(0); // captured == "abcdef"
|
QString captured = match.captured(0); // captured == "abcdef"
|
||||||
//! [18]
|
//! [18]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [19]
|
//! [19]
|
||||||
QRegularExpression re("abc|ab");
|
QRegularExpression re("abc|ab");
|
||||||
QRegularExpressionMatch match = re.match("ab", 0, QRegularExpressionMatch::PartialPreferFirstMatch);
|
QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
|
||||||
bool hasMatch = match.hasMatch(); // false
|
bool hasMatch = match.hasMatch(); // false
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // true
|
bool hasPartialMatch = match.hasPartialMatch(); // true
|
||||||
//! [19]
|
//! [19]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [20]
|
//! [20]
|
||||||
QRegularExpression re("abc(def)?");
|
QRegularExpression re("abc(def)?");
|
||||||
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpressionMatch::PartialPreferFirstMatch);
|
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
|
||||||
bool hasMatch = match.hasMatch(); // false
|
bool hasMatch = match.hasMatch(); // false
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // true
|
bool hasPartialMatch = match.hasPartialMatch(); // true
|
||||||
//! [20]
|
//! [20]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [21]
|
//! [21]
|
||||||
QRegularExpression re("(abc)*");
|
QRegularExpression re("(abc)*");
|
||||||
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpressionMatch::PartialPreferFirstMatch);
|
QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
|
||||||
bool hasMatch = match.hasMatch(); // false
|
bool hasMatch = match.hasMatch(); // false
|
||||||
bool hasPartialMatch = match.hasPartialMatch(); // true
|
bool hasPartialMatch = match.hasPartialMatch(); // true
|
||||||
//! [21]
|
//! [21]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [22]
|
//! [22]
|
||||||
QRegularExpression invalidRe("(unmatched|parenthesis");
|
QRegularExpression invalidRe("(unmatched|parenthesis");
|
||||||
bool isValid = invalidRe.isValid(); // false
|
bool isValid = invalidRe.isValid(); // false
|
||||||
//! [22]
|
//! [22]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [23]
|
//! [23]
|
||||||
QRegularExpression invalidRe("(unmatched|parenthesis");
|
QRegularExpression invalidRe("(unmatched|parenthesis");
|
||||||
if (!invalidRe.isValid()) {
|
if (!invalidRe.isValid()) {
|
||||||
@ -237,44 +272,62 @@ if (!invalidRe.isValid()) {
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [23]
|
//! [23]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [24]
|
//! [24]
|
||||||
QRegularExpression re("^this pattern must match exactly$");
|
QRegularExpression re("^this pattern must match exactly$");
|
||||||
//! [24]
|
//! [24]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [25]
|
//! [25]
|
||||||
QString p("a .*|pattern");
|
QString p("a .*|pattern");
|
||||||
QRegularExpression re("\\A(?:" + p + ")\\z"); // re matches exactly the pattern string p
|
QRegularExpression re("\\A(?:" + p + ")\\z"); // re matches exactly the pattern string p
|
||||||
//! [25]
|
//! [25]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [26]
|
//! [26]
|
||||||
QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
|
QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
|
||||||
// escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)"
|
// escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)"
|
||||||
//! [26]
|
//! [26]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QString name;
|
||||||
|
QString nickname;
|
||||||
//! [27]
|
//! [27]
|
||||||
QString pattern = "(" + QRegularExpression::escape(name) +
|
QString pattern = "(" + QRegularExpression::escape(name) +
|
||||||
"|" + QRegularExpression::escape(nickname) + ")";
|
"|" + QRegularExpression::escape(nickname) + ")";
|
||||||
QRegularExpression re(pattern);
|
QRegularExpression re(pattern);
|
||||||
//! [27]
|
//! [27]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QString string;
|
||||||
|
QRegularExpression re;
|
||||||
//! [28]
|
//! [28]
|
||||||
QRegularExpressionMatch match = re.match(...);
|
QRegularExpressionMatch match = re.match(string);
|
||||||
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
|
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
|
||||||
QString captured = match.captured(i);
|
QString captured = match.captured(i);
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [28]
|
//! [28]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [29]
|
//! [29]
|
||||||
QRegularExpression("(\\d\\d) (?<name>\\w+)");
|
QRegularExpression re("(\\d\\d) (?<name>\\w+)");
|
||||||
QRegularExpressionMatch match = re.match("23 Jordan");
|
QRegularExpressionMatch match = re.match("23 Jordan");
|
||||||
if (match.hasMatch()) {
|
if (match.hasMatch()) {
|
||||||
QString number = match.captured(1); // first == "23"
|
QString number = match.captured(1); // first == "23"
|
||||||
QString name = match.captured("name"); // name == "Jordan"
|
QString name = match.captured("name"); // name == "Jordan"
|
||||||
}
|
}
|
||||||
//! [29]
|
//! [29]
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
//! [30]
|
//! [30]
|
||||||
// extracts the words
|
// extracts the words
|
||||||
QRegularExpression re("(\\w+)");
|
QRegularExpression re("(\\w+)");
|
||||||
@ -285,5 +338,6 @@ while (i.hasNext()) {
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
//! [30]
|
//! [30]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user