Regular expressions? What is a regular expression?
The explanation given by Baidu Encyclopedia is this: regular expressions use a single string to describe and match a series of strings that conform to a certain syntactic rule.
According to my study, I understand the regular expression is: A string, this string is used to describe the rules we make.
Learning regular expressions is like learning a new language, but the language is used to make rules. To learn a new language, we usually start from HelloWorld.
For example, if we were to find hellowrold in a very long string, we could use the regular expression HelloWorld. HelloWorld This regular expression is the simplest type of regular expression that matches such a string: A string arranged in the order of HelloWorld.
Here are some special codes in regular expressions, also called metacharacters (metacharacter). In my opinion, these metacharacters resemble the keywords in our OC language. (Of course, the regular expression and character certainly more than that point, but I think it is enough for us to develop the use of the)
\b: Represents the beginning or end of a word as a dividing point of a word.
For example: The regular expression H I matches words that contain the two consecutive letters of hi, like history, him. And if you add \b, the regular expression becomes the \bhi\b then it will only match the word hi.
. (Period in English): matches any character except newline characters.
For example: the regular expression \ba.\b (\b is a meta-character that represents the beginning or end of a word, as described above) matches to a string preceded by a character A, followed by an arbitrary character, except for a newline character, such as AA, AB, A_, A +, which can be matched to
\d: Match a number (0-9)
For example, the regular expression 0\d\d\d\d\9 matches a string of 6 digits ending in 9 with 0, and four digits in the middle can be any number from 0 to 9, like 056789, 043629, etc.
\w: matches letters or numbers or underscores or kanji
For example: the regular expression \w\w\w\w matches any four letters or numbers or underscores or Chinese characters, such as idioms in Chinese: ten-action rejection, general exultation Ben This type, or is: Main, void this four-letter word, or is:5_a, trench B6 this type of hybridization
\s: Match any white space character (above said so much, this has nothing to say, should be very good understanding of the put)
PS: The metacharacters described above is used to match a particular format or character, while the following few are used to match the number of
* (asterisk): repeat 0 or more times
For example, the regular expression a\d matches a string that begins with the character a followed by 0 or more numbers: A, A1, a94803748 these are strings that match the regular expression;
+: Repeat 1 or more times
For example, the regular expression a\d+ matches a string that begins with the character A, followed by 1 or more numbers: A3, a5856306, but does not match a should be + represents at least one;
? : Repeat 0 or 1 times
Similar to the above two, expression a\d? Matches a string beginning with the character a followed by 0 or a number: A, A5, A9
{n}: repeat n times
For example: Our ID number, generally 18 digits (in fact, the verification of ID card is much more complex, here is just a chestnut), then we can write a regular expression to match it: \d{18}, or for example, our bank card withdrawal password, generally 6 digits, then we can write: \d{6} to match a regular expression like this.
{n,}: Repeat N or more times (that is, at least repeat n times)
For example: Now many Web site account registration, password requirements not less than how many number, then we can write: \d{6,}, this is not less than 6 digits
{n,m}: Repeats N to M times (repeats at least n times, up to M times)
For example: QQ account requirements are 5 to 12 bits, then you can use regular expressions: \d{5,12} to match
Well, the above introduction to the meta-character can basically meet our development requirements.
Then there are a few points to note:
[] The use of:
[abcdef] This regular expression matches one of the letters in the ABCDEF, [.?!] This regular expression matches one of the three punctuation marks inside. We can also specify a range, [0-9] to match any number in the interval from 0 to 9 (including 0 and 9). [A-z] here is one of the letters that matches the range A to Z. We can also use these ranges together, for example [0-9a-za-z] is one of the characters or numbers in one of the three intervals that match 0 to 9, A to Z, A to Z.
branching conditions: Sometimes we need a "or" sign to connect two forms when we want to make rules that come in many forms.
For a chestnut: Our phone number can be expressed in two forms: 020-88888888 (020) 88888888. To let the two forms of writing can match, we will write two forms of regular expressions, matching 020-88888888 of the regular expression can be written as: 0\d{2}-\d{8}; Matches (020) 88888888 Regular Expressions We can write: (0\d{2}) \d{ 8}. If you want these two regular expressions to match a string together, you'll use the "or" symbol to connect them: 0\d{2}-\d{8}| (0\d{2}) \d{8}.
PS: Other languages I am not very clear, but in OC "|" The operator is to judge the previous expression, and if the previous expression is "true", the subsequent operation will not proceed, and the regular expression "|" The operator is the same, as long as the preceding expression is ture, it will not continue to judge the following operation. For example: \d{3}|\d{3}-\d{4} This regular expression, because the previous \d{3} section and the following \d{3} section, when the string 111-11 matches only to the previous three bits, the following "11" are not matched. If you're not ready to go beyond iOS, I recommend this as an understanding.
Group:
It is easier to use parentheses to "()" to enclose some of these statements as a whole.
For example: [A-za-z0-9][email protected][a-za-z0-9]+ (. [ a-za-z0-9]{2,4}) {n} This is a regular expression of an imperfect mailbox match, the following section "(. [ a-za-z0-9]{2,4}) {"You can see the section enclosed in parentheses.") [A-za-z0-9] {2,4}) " This means that they are a whole, with the following "{1}" repeating for a total of two times, relative to the parentheses. The entire regular expression matches a mailbox like this: [email protected] or matching: [email protected] Such a mailbox (although I do not know whether the mailbox suffix exists, but these are not the focus).
Here, we can look at 2 examples to confirm what is said above:---------------------------------
(\d{1,3}.) {3}\d{1,3}: is a simple IP-address matching expression. To understand this expression, parse it in the following order: \d{1,3} matches a number from 1 to 3 digits, (\d{1,3}.) {3} matches three digits plus an English period (this whole is the group ) repeats 3 times, and finally adds a one to three digits (\d{1,3}). (This regular expression is not really accurate, the exact IP address description is this: (2[0-4]\d|25[0-5]|[ 01]?\d\d?).) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?] )
(? 0\d{2})? [-]?\d{8}|0\d{2}[-]?\d{8} This expression matches the phone number of the 3-bit area code, where the area code can be enclosed in parentheses or not, the area code and the local number can be separated by a hyphen or space, or there can be no interval.
If the above two examples, you can analyze it yourself, then basically enough. If you want to learn more about regular expressions, there's no way to help you, but Mom is sure to help you. Http://www.jb51.net/tools/zhengze.html and my own words are on this website to understand, interested can go to see.
At last I almost forgot, and one more thing: character transfer
If you want to find the meta-character itself, such as when you look up, or *, there's a problem: You can't specify them, because they'll be interpreted as something else. Then you have to use \ To cancel the special meaning of these characters. Therefore, you should use. and *. Of course, to find \ itself, you also have to use \.
For example: Deerchao.net matches deerchao.net,c:\windows matching C:\Windows.
If matching a number string is not a phone number, you can write a class to NSObject and add the following methods:
-(BOOL) Isphonenumberexistwithphonenumber: (NSString *) phonenumber { *teststr = phonenumber; @ "^1 (3[0-9]|5[0-35-9]|8[0-25-9]|70|77) \\d{8}$"; *pred = [Nspredicate predicatewithformat:@ "selfmatches%@", regex]; = [pred evaluatewithobject:teststr]; return IsMatch;}
Wen/langlangago (author of Jane's book)
Original link: http://www.jianshu.com/p/94bd7327f735
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
ios--Regular Expressions