iOS Regular expressions

Source: Internet
Author: User
Tags alphabetic character character classes

1 The syntax of the regular expression also has some reserved words ([,(and),\,*,+,?,{and},^,$,.,| (pipe),/). These characters are used as advanced pattern matching. If you want to search for one of these characters, you need to escape it with a backslash (\), for example, to search for a period in a block of text, insteadof using a.

2 intercept parentheses (capturing parentheses) are used as part of the group pattern. For example:3 (pm|am) matches the text "3 pm" and also matches "3 am". The vertical bar character (|) is performed or manipulated. You can include more than one vertical bar character in your regular expression, as long as you are happy. For example, (tom| dick| Harry) is an effective pattern that matches any of the three names.

The 3 -character group (Character classes) is equivalent to a single character in a set of characters. Character groups appear between brackets ([ and ]). For example, the regular expression T[aeiou] will match "Ta", "TE", "Ti", "to" or "tu". You can put any number of characters in brackets, but keep in mind that only one character is matched. [AEIOU] looks like five characters, but it really means "a" or "E" or "I" or "O" or "U". If the characters appear continuously, you can also define a range in the character group. For example, in order to search for numbers from 100 to 109, the pattern should be 10[0-9]. For example, the pattern T[^o] will match characters that contain "T" and the characters immediately following it are non-O.

4 . matches any one character. p.p match pop,pup,pmp,[email protected] and so on.

 \w matches any "word-like" character, including numbers, letters, and underscores, but does not match punctuation and other characters. hello\w matches "Hello_", "Hello9" and "Helloo", but does not match "hello!".

 \d matches numbers, in most cases [0-9]. \d\d?:\ D\d matches strings in the time format, such as "9:30" and "12:45".

 \b matches extra characters, such as spaces, punctuation marks. to\b will match "to the Moon" and "to!" "To", but does not match "tomorrow". \b is used in the whole word matching and convenient.

 \s matches whitespace characters, such as spaces, tabs, and line breaks. hello\s will match "Well,hello there!" "Hello" in the.

 ^ used at the beginning of a line. Remember, this special ^ is different from the ^in square brackets! For example,^hello matches the string "Hello there" instead of matching "He said hello".

 $ used at the end of a line, for example, theend$ will match "It was the end" without going to match "the end is near".

 * Match it before the element 0 or more times. 12*3 will match 13, 123, 1223, 122223, and 1222222223.

 + matches the element before it 1 or more times. 12+3 will match 123, 1223, 122223, and 1222222223.

 The curly brace {} contains the maximum and minimum number of matching values. For example,10{1,2}1 matches "101" and "1001" Without Matching "10001" because the minimum number of matches is 1 and the maximum number is 2. He[li]{2,}o will match "Hello" and "Helllllio" and any other "hello" to add multiple L variants, so there is no limit, because the minimum number is 2, the maximum number is not set.

? Matches the preceding subexpression 0 or one time. For example, "Do (es)?" can match "do" in "do" or "does".? Equivalent to {0,1}.

{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.

{n,} N is a non-negative integer. Match at least N times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.

{n,m} Both m and n are non-negative integers, where n <= m. Matches at least N times and matches up to M times. Liu, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.

? When the character immediately follows any other restriction (*, +,?, {n}, {n,}, {n,M}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' will match a single "O", while ' o+ ' will match all ' o '.

X|y matches x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches "Zood" or "food".

[XYZ] Character set. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '.

[^xyz] negative character set. Matches any character that is not contained. For example, ' [^ABC] ' can match ' P ' in ' plain '.

A [A-Z] character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the ' a ' to ' Z ' range.

[^ A-z] negative character range. Matches any character that is not in the specified range. For example, ' [^a-z] ' can match any character that is not within the range of ' a ' to ' Z '.

\b Matches a word boundary, which is the position between a word and a space. For example, ' er\b ' can match ' er ' in ' never ', but not ' er ' in ' verb '.

\b Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.

\CX matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is treated as a literal ' C ' character.

\d matches a numeric character. equivalent to [0-9].

\d matches a non-numeric character. equivalent to [^0-9].

\ n matches a line break. Equivalent to \x0a and \CJ.

\s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].

\w matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.

\w matches any non-word character. Equivalent to ' [^a-za-z0-9_] '.

 

iOS Regular expressions

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.