Go to the Regular Expression and qregexp of QT.

Source: Internet
Author: User
Consider the problems we often encounter. For example, gemfield wants to find a key phone number from www.civilnet.cn/book, generally, the first step is to find all the phone numbers in the book at hand. So how can we determine the query conditions? The telephone format is as follows:
01088888888
010 88888888
010-88888888
88888888
0532-88888888
0534-8888888
88888888 Beijing
Qingdao88888888
............
Ellipsis indicates that there are many types of formats, but there are also more clearly not the telephone type. In this case, how can we formulate our query conditions. How many times can I obtain the if or switch branch in the code.
Gemfield hopes that a statement can briefly describe all the possible formats above. This may be the source of a regular expression. The regular expression is a regular expression. Regular Expressions are generally translated into regular expressions, regular expressions, and regular expressions ". Here, "regular" refers to "rules" and "rules", and "Regular Expression" refers to "expressions that describe certain rules.
Let's see how gemfield implements this idea step by step.

We can use
Ddd-dddddddd or ddddddwwwwwww
To describe examples such as 010-88888888 and 888888beijing.

Here, D indicates all possible numbers: 0, 1, 2, 3, 4 ...... 9; W indicates all possible letters: A, B, C, D ...... Z.
But sometimes we have a definite limit on this position: 'w', rather than representing the W of all letters. What should we do? The example above will certainly bring confusion. We use the backslash/to implement:
/D-/D or/D/d /w
In this way, we can tell whether W represents all letters or only w itself. But it looks a little ugly. What should I do if I lose the wrong number or read the wrong number for such a long time? Gemfield can be simplified using the following code:
/D3-/d8 or/D8/W7
However, the problem arises again. Here, 3 indicates that the preceding number appears three times, not 3. The numbers 8 and 7 are the same. What should we do? Gemfield can be written as follows:
/D {3}-/d {8} Or/d {8}/W {7}
Here {} is similar to the backslash. We clearly tell you that W is not w, 3 is not 3. :-D
However, it is obvious that some telephone numbers do not contain area numbers, such as 010-88888888 writing 88888888. In this way, it is not/d {3}-/d {8}, but/d {8 }. Maybe you can write:
/D {8} Or/d {3}-/d {8}
However, "or" cannot be a keyword in the program language. No compiler will recognize it. What should I do? We thought that there is an operator in the program that means similar to "or". It is |. Gemfield can write:
/D {8} |/d {3}-/d {8}
But it seems like there are only a few things. It seems that they are mixed together and cannot be distinguished. We thought of parentheses:
(/D {8}) | (/d {3}-/d {8 })
Enclose it in parentheses to indicate a whole. The meaning here is that there are two sets of patterns, which of them can be. Traditionally ,? This symbol (question mark) can be used to indicate that an item is optional. In this case, the above expression can be written:
(/D {3 }-)? (/D {8 })
This indicates that the previous Group (/d {3}-) is optional, that is, it can either have or do not, which just expresses the intention of gemfield. We also need to consider the optional space because of the format of 010 88888888. For spaces, we can use/s to represent them. The expression is modified as follows:
(/D {3 })? (-)? /S? (/D {8 })
However, the space in the middle of the phone number 010 88888888 is also a lot of input, such
10 88888888, what should we do? We use * to represent 0, 1, 2, or more. The modification is as follows:
(/D {3 })? (-)? /S * (/d {8 })
However, it seems that the phone number is different from that in Beijing. There are eight numbers (except the area code), and there are still seven in most Chinese cities. Therefore, this quantity must be optional. We can expand the following:
(/D {3 })? (-)? /S * (/d {7, 8 })
In this way, the number is 7 or 8, that is, 7 or 8.
Well, although the above evolution has no significance for the rigorous syntax, with this idea, we can get to know our qregexp. Welcome to the QT regular expression-qregexp.
**************************************** ************
A regular expression is composed of statements, numbers, and delimiters.
The statement is the simplest. It is a complete substatement in. For example, [ABCD] matches letters a, B, c, or D, and [A-Z] represents 26 uppercase letters.
A {} matches one, two, and three ...... 26 letters;
[0-9] {1, 2} matches 0 ~ 99, but also matches ab34 and a34b;
^ [0-9] {1, 2} matches 34bc and 26 ABCD, as long as there is nothing before the number;
[0-9] {1, 2} $ matches ab65 and aaaaa56, as long as there are other things behind the number;
^ [0-9] {1, 2} $ can only be 2 digits.
But ^ is different once it appears in square brackets. It indicates "not included ".
For example, [^ ABC] matches everything except A, B, or C.
+ Indicates that a appears at least once. For example, [ABC] + indicates that A, B, or C appears at least once.
As *,? It is the same as the reasoning of the initial part in gemfield.
**************************************** **************
How does the qregexp class use these Regexp statements? gemfield has two general cases, which are exactly two sides of the transaction. The first type is "Search. Let's look at an example:
**************************************** **************
STR = "civilnet Corporation/tcivilnet.cn/tgele ";
Qstring company, web, country;
Rx. setpattern ("^ ([^/T] +)/t ([^/T] +)/t ([^/T] +) $ ");
If (RX. indexin (STR )! =-1 ){
Company = Rx. CAP (1 );
Web = Rx. CAP (2 );
Country = Rx. CAP (3 );
}
**************************************** **************
As the code snippet reveals above, the indexin and cap functions are more common, as to the specific meaning, you can read QT online documentation: http://www.civilnet.cn/book/embedded/GUI/Qt_assistant/index.php

The second type is "forbidden". For example, a qlineedit prohibits the input of something, such as a mailbox name. This is implemented using qregexpvalidator. This class receives a qregexp-type regular expression as the parameter during instantiation:
**************************************** ******************
Qregexp rx ("-? // D {1, 3 }");
Qvalidator * validator = new qregexpvalidator (RX, this );

Qlineedit * edit = new qlineedit (this );
Edit-> setvalidator (validator );
**************************************** ********************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.