Quote http://www.cnblogs.com/sld666666/archive/2011/03/16/1985715.html
1. Common regular expressions
expression | illustrate |
\r, \n | Represents carriage return and line feed |
\t | Tabs |
\\ | stands for "\" itself |
\^ | matches the ^ symbol itself |
\$ | matches the $ sign itself |
metacharacter | illustrate |
. | matches any character except newline |
\w | Match letters, numbers, underscores, Chinese characters |
\s | matches any whitespace |
\b | start or end of a word |
\~ | matches the beginning of the string |
$ | Matches the end of the string |
Such as:
\ba\w*\b : matches words starting with the letter a - the beginning of a word (\b), then the letter a, then any number of letters or numbers (\w*), and finally the word end (\b).
\d+ : Match 1 or more consecutive digits. Here + is a metacharacter similar to *, except that * matches any number of repetitions (possibly 0), while + matches 1 or more repetitions.
\b\w{6}\b: Match words of exactly 6 characters.
expression | illustrate |
[ ] | contains a sequence of characters |
[^ ] | a sequence of characters other than |
[ab5@]: matches "a" or "b" or "5" or "@"
[^abc]: contains any character other than abc
[fk]: any character between fk
expression | illustrate |
{n} | The expression is repeated n times, for example: "\w{2}" is equivalent to "\w\w" ; "a{5}" is equivalent to "aaaaa" |
{m,n} | The expression is repeated at least m times and at most n times, for example: "ba{1,3}" can match "ba" or "baa" or "baaa" |
{m,} | The expression must be repeated at least m times, for example: "\w\d{2,}" can match "a12","_456","M12344"... |
? | Match the expression 0 or 1 times, equivalent to {0,1}, for example: "a[cd]?" can match "a", "ac", "ad" |
+ | The expression appears at least once, equivalent to {1,}, for example: "a+b" can match "ab", "aab", "aaab"... |
* | The expression does not appear or appears any number of times, equivalent to {0,}, for example: "\^*b" can match "b", "^^^b"... |
2. Using regular expressions in Qt
Qt uses QRegExp to encapsulate regular expressions. Such as: QRegExp rx("^[0-9]+(\\.\\d+)?$");
For example, we have another regular string: Price: Quantity: Merchant Code: Attribute Name: Attribute Value; Attribute Name: Attribute Value; Price: Quantity: Merchant Code: Attribute Name: Attribute Value; Attribute Name: Attribute Value; Attribute name: attribute value; price: quantity: merchant code: attribute name: attribute value;
Want to split into:
Price: Quantity: Merchant Code: Attribute Name: Attribute Value; Attribute Name: Attribute Value;
Price: Quantity: Merchant Code: Attribute Name: Attribute Value; Attribute Name: Attribute Value; Attribute Name: Attribute Value;
Price: Quantity: Merchant Code :property name:property value;
First choose a suitable regular expression: (\w*:){3}(\w*:\w*;)+, then:
QRegExp rx(tr("(\\w*:){3}(\\w*:\\w*;)+"));
int pos(0);
while ((pos = rx.indexIn(str, pos)) != -1)
{
strList.push_back(rx.capturedTexts().at(0));
pos += rx.matchedLength();
}
strList 就是想要的结果了