QRegExp 成员函数介绍

1.QString QRegExp::cap(int nth = 0) const

Returns the text captured by the nth subexpression. The entire match has index 0 and the parenthesized subexpressions have indexes starting from 1 (excluding non-capturing parentheses).

返回第 n 个子表达式捕获的文本。整个匹配的索引为 0,带括号的子表达式的索引从 1 开始(不包括非捕获括号)。

1
2
3
4
5
6
7
QRegExp rxlen("(\\d+)(?:\\s*)(cm|inch)");
int pos = rxlen.indexIn("Length: 189cm");
if (pos > -1) {
QString value = rxlen.cap(1); // "189"
QString unit = rxlen.cap(2); // "cm"
// ...
}

The order of elements matched by cap() is as follows. The first element, cap(0), is the entire matching string. Each subsequent element corresponds to the next capturing open left parentheses. Thus cap(1) is the text of the first capturing parentheses, cap(2) is the text of the second, and so on.

cap() 匹配的元素顺序如下。第一个元素 cap(0) 是整个匹配字符串。每个后续元素对应于下一个捕获左括号。因此 cap(1) 是第一个捕获括号的文本,cap(2) 是第二个捕获括号的文本,依此类推。

2.bool QRegExp::exactMatch(const QString &str) const

Returns true if str is matched exactly by this regular expression; otherwise returns false. You can determine how much of the string was matched by calling matchedLength().
For a given regexp string R, exactMatch(“R”) is the equivalent of indexIn(“^R$”) since exactMatch() effectively encloses the regexp in the start of string and end of string anchors, except that it sets matchedLength() differently.
For example, if the regular expression is blue, then exactMatch() returns true only for input blue. For inputs bluebell, blutak and lightblue, exactMatch() returns false and matchedLength() will return 4, 3 and 0 respectively.

如果 str 与此正则表达式完全匹配,则返回 true; 否则返回假。 您可以通过调用matchedLength() 来确定字符串的匹配程度。
对于给定的正则表达式字符串 R,exactMatch(“R”) 等效于 indexIn(“^R$”),因为exactMatch() 有效地将正则表达式包含在字符串的开头和字符串锚点的结尾,除了它设置matchedLength( ) 不同。
例如,如果正则表达式为蓝色,则exactMatch() 仅对输入蓝色返回true。 对于输入 bluebell、blutak 和 lightblue,exactMatch() 返回 false,matchedLength() 将分别返回 4、3 和 0。

3.int QRegExp::indexIn(const QString &str, int offset = 0, QRegExp::CaretMode caretMode = CaretAtZero) const

Attempts to find a match in str from position offset (0 by default). If offset is -1, the search starts at the last character; if -2, at the next to last character; etc.
Returns the position of the first match, or -1 if there was no match.
The caretMode parameter can be used to instruct whether ^ should match at index 0 or at offset.
You might prefer to use QString::indexOf(), QString::contains(), or even QStringList::filter(). To replace matches use QString::replace().

尝试从位置偏移量(默认为 0)在 str 中找到匹配项。 如果 offset 为 -1,则从最后一个字符开始搜索; 如果 -2,则在倒数第二个字符处; 等等
返回第一个匹配的位置,如果没有匹配则返回 -1。
caretMode 参数可用于指示 ^ 是否应在索引 0 处匹配或在偏移处匹配。
你可能更喜欢使用 QString::indexOf()、QString::contains(),甚至是 QStringList::filter()。 要替换匹配项,请使用 QString::replace()。

1
2
3
4
5
6
7
8
9
QString str = "offsets: 1.23 .50 71.00 6.00";
QRegExp rx("\\d*\\.\\d+"); // primitive floating point matching
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
++count;
pos += rx.matchedLength();
}
// pos will be 9, 14, 18 and finally 24; count will end up as 4

Although const, this function sets matchedLength(), capturedTexts() and pos().
If the QRegExp is a wildcard expression (see setPatternSyntax()) and want to test a string against the whole wildcard expression, use exactMatch() instead of this function.

虽然返回值是 const,但该函数设置了matchedLength()、cappedTexts() 和pos()。
如果 QRegExp 是通配符表达式(请参阅 setPatternSyntax())并且想要针对整个通配符表达式测试字符串,请使用 exactMatch() 而不是此函数。

示例

示例1:匹配、获取小括号内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
QString text = "qw2(1,2,3,4)223(342)32(1,,3,4)31we"

QRegExp re("\\(.*\\)"); // 括号及括号内部的任何字符。
if (re.indexIn(text, 0) >= 0) {
auto ret = re.cap(0); // ret = "(1,2,3,4)223(342)32(1,,3,4)"
}

QRegExp re("\\([^)]*\\)"); // 从一个开括号到最近的闭括号。
int pos = 0;
QStringList list;
while ((pos = exp.indexIn(text, pos)) != -1) {
list << exp.cap(1);
pos += exp.matchedLength();
}
qDebug() << list; // (1,2,3,4), (342), (1,,3,4)

QRegExp rx("(\\d+)");
QString str = "Offsets: 12 14 99 231 7";
QStringList list1;
pos = 0;

while ((pos = rx.indexIn(str, pos)) != -1) {
list1 << rx.cap(1);
pos += rx.matchedLength();
}
qDebug() << "list1:" << list1;
// list1: ["12", "14", "99", "231", "7"]

示例2:匹配内容

全内容匹配,使用 exactMatch()

1
2
3
4
5
6
QString text = "(1,2,3,4)";
QString text1 = "11(1,2,3,4)";

QRegExp exp("\\(.*\\)");
qDebug() << exp.exactMatch(text); // true
qDebug()<< exp.exactMatch(text1); // false