The QT SDK contains a helpful GUI tool that allows us to do this kind of conversion and test your expression. You can open it in the following way: "Start"-> "->" Qt SDK by Nokia v2010.02.1 (open source) "->" Qtdemo "->" Tools "->" RegularExpression "Launch" can be selected to start the program, you can also "documentation" view the source file. |
qregexp::qregexp (const QREGEXP & Rx)/Create a regular expression that is a copy of the RXThe regular expression qregexp regexp ("Taskctll"), which is established in the program to match the node reboot, is the matching of the expression with taskctll.
qregexp::qregexp (const QString & pattern, qt::casesensitivity cs = qt::casesensitive, patternsyntax syntax = Reg EXP)//To establish a regular expression object based on a given pattern string (patterns string), the pattern string must be given in the form of a wildcard character (wildcard)The hungry Regular expression qregexp regexpparent ("parent\\[0\\]\\s+\\-{0,1} (\\d+) \\b"), which is established in the program to match the first parent node;
int qregexp::indexin (const QString & str, int offset = 0, Caretmode caretmode = Caretatzero) const//attempt to offset from position to 0 (default Recognition) out to find a matching character entered prompt str, returns the position of the first match position, if no match is returned-1The program only needs to match once (only one of each row in the text file has a repeat str), and the information is extracted and saved as long as the matching return value is not-1
There are some differences between the regular expressions in Qt and C + +, such as
\\.Said
. \\d says D
Program instance:
Match clock Jump2011/06/27 22:05:42.011 parent[1]-1 2011/06/27 22:05:42.011 local time 2d5820d ":(\\d+) \ \. (\\d+) \\s+ (\\w*\\s*\\w*\\s*\\d*\\,\\s*) * (\\w*\\s*) * (parent\\[[01]\\]\\s+\\-{0,1}\\d+) * (\\w*\\d*\\.\\d*\\w*\\s* \\d*) *local\\s+time "
Match first parent node2011/06/27 22:05:42.011 parent[0] 6003 "parent\\[0\\]\\s+\\-{0,1} (\\d+) \\b"
Match Node Number2011/06/27 22:26:30.090 mac Neigh 0, addr "mac\\s+neigh\\s+0\\,\\s+addr\\s+ (\\d+) \\b"
The root node receives the data2011/06/27 22:07:45.058 ROOT receive Data origin "root\\s+receive\\s+\\dada\\s+\\origin\\s+ (\\d+) \\s+ (\\d+) \\b"
Regular expressions of common use
An expression |
Description |
\ r, \ n |
Represents carriage return and line feed |
\ t |
Tabs |
\\ |
Represents the "\" itself |
\^ |
Match ^ symbol itself |
\$ |
Match $ symbol itself |
Metacharacters |
Description |
. |
Matches any character other than a line break |
\w |
Match letters, numbers, underscores, Chinese characters |
\s |
Match any white space character |
\b |
The beginning or end of a word |
\~ |
Match the start of a string |
$ |
End of Match string |
Such as:
\ba\w*\b: Matches words that begin with the letter A--first at the beginning of a word (\b), then the letter A, then any number of letters or numbers (\w*), and finally at the end of the word (\b).
\d+: Matches 1 or more consecutive digits. Here the + is and * similar to the meta character, the difference is the * match repeat any time (may be 0 times), and + will match repeat 1 or more times.
\b\w{6}\b: Matches a word that is just 6 characters.
An expression |
Description |
[ ] |
Contains a series of characters |
[^ ] |
Contains a series of characters outside the |
|
|
[ab5@]: Matches "a" or "B" or "5" or "@"
[^ABC]: contains any character other than ABC
[F-k]: any character between f-k
An expression |
Description |
N |
Expression repeats n times, for example: "\w{2}" is equivalent to "\w\w"; "a{5}" is equivalent to "AAAAA" |
{M,n} |
The expression repeats at least m times and repeats at most n times, for example: "ba{1,3}" can match "ba" or "Baa" or "baaa" |
{m,} |
The expression repeats at least m times, for example: "\w\d{2,}" can match "A12", "_456", "M12344" ... |
? |
Match expression 0 or 1 times, equivalent to {0,1}, such as: "A[CD]?" Can match "a", "AC", "AD" |
+ |
Expression appears at least 1 times, equal to {1,}, for example: "A+b" can match "AB", "AaB", "Aaab" ... |
* |
The expression does not appear or appear any time, equal to {0,}, for example: "\^*b" can match "B", "^^ ^b" ... * the number of characters in the previous occurrence is arbitrary |
Note: In a regular expression, the front bracket [belongs to a special character, to match it, you need to escape, that is, plus \; The bracket] is not a special character, and you do not have to add an escape character. var pattern = "(\[{1})";//error, correct should be: ([\[]{1}) var flag = "GI"; var reg = new Rege XP (pattern, flag); reg.exec ("<>[]"); alert (regexp.$1); The above code is incorrect, error in pattern, should be written ([\[]{1}). That is: for front brackets [, except for escape characters, you should also enclose them in brackets. seems unusual in the handling of the parentheses. var pattern = "([\]]{1})"; Error, correct should be: (]{1}) var flag = "GI"; var reg = new RegExp (pattern, flag); reg.exec ("<>[]");   ; Alert (regexp.$1); The above code is correct in IE, but in Chrome, it does not get the results we want, error is also in pattern, should be written (]{1}). That is: for the back bracket, it should be written directly, neither with the escape character nor in brackets. data: special characters in regular expressions (need to be escaped) ^ matches the starting position of the input string, unless used in a bracket expression, at which point it means that the character set is not accepted. To match the ^ character itself, use \^.