Let's talk about regular expressions. A regular expression is introduced in as3.0. A regular expression is used to verify whether a text value conforms to a specific pattern.
A regular expression has four parts. 1 child character. 2-element sequence. 3 flag. 4. Number of characters.
We can see that pattern Regexp =/matches the string metacharacter sequence/marker. Of course, the sequence may not be exactly like this. Don't make his mind too complicated. In fact, he is a combination.
Now let's take a look at metacharacters: ^ $ \. * +? () [] {} |
The preceding section describes the use of escape strings.
1. ^ Usage: matches the start of a string
VaR pattern: Regexp =/^ bug /;
VaR STR: String = "worms are good people ";
Trace (STR, "is valid:", pattern. Test (STR); bug is a good guy is valid: True
2. $ Usage: matches the end of a string
VaR pattern: Regexp =/Lizhen $ /;
VaR STR: String = "My name is Lizhen ";
Trace (STR, "is valid:", pattern. Test (STR); bug is a good guy is valid: True
Take a closer look at the position of the two symbols in the matching string. Note: if both the ^ and $ symbols are used, exact match will be performed.
3. \ Usage: matches the end of a string
If the matching string contains "/" such as 1/2, use the following definition:
VaR pattern: Regexp =/1 \/2 /;
VaR STR: String = "1/2 what is it? ";
Trace (STR, "is valid:", pattern. Test (STR); 1/2? Is valid: True
If the matching string contains "" such as "Lizhen", use the following definition:
VaR pattern: Regexp =/\ "Lizhen "/;
VaR STR: String = "My name is \" Lizhen \"";
Trace (STR, "is valid:", pattern. Test (STR); my name is "Lizhen" is valid: True
4. * Usage: matches 0 or more first characters.
VaR pattern: Regexp =/my */;
VaR STR: String = "My my"; or var STR: String = "my"
Trace (STR, "is valid:", pattern. Test (STR); my... is valid: True
5. + Usage: match at least one character before
VaR pattern: Regexp =/My + /;
VaR STR: String = "My ID is..."; or var STR: String = "my"
Trace (STR, "is valid:", pattern. Test (STR); My ID is... is valid: True
Note the difference between + and. If var STR: String = "my", false is displayed.
6.? Usage: Match 0 or 1 Character
VaR pattern: Regexp =/day? /;
VaR STR: String = "happy every day"; or var STR: String = "happy every day ";
Trace (STR, "is valid:", pattern. Test (STR); happy day is valid: True
7.. Usage: match any single character
VaR pattern: Regexp =/day ./;
VaR STR: String = "happy every day"; or var STR: String = "happy every day ";
Trace (STR, "is valid:", pattern. Test (STR); happy day is valid: True
8. [] Usage: match a specific range, not limited to a specific single character
VaR pattern: Regexp =/[A-Z]/
VaR STR: String = ""
Trace (STR, "is valid:", pattern. Test (STR); a is valid: True
9. () Usage: This is equivalent to a set.
VaR pattern: Regexp =/([A-Z] [A-Z] [0-9]) +/For example, this defines a four-character string. The first character is the character between A and Z, the second is the character between A and Z, and the third is the character between 0-9, the fourth digit repeats the preceding number.
VaR STR: String = "ad77aaaaaaaaaaaaaaaa"
Trace (STR, "is valid:", pattern. Test (STR); ad77aaaaaaaaaaaaaa is valid: True
10. | Usage: match the characters on the left or right.
VaR pattern: Regexp =/bug | Lizhen/
VaR STR: String = "worms are good people"
Trace (STR, "is valid:", pattern. Test (STR); bug is a good guy is valid: True
Let's talk about it nowMeta-SequenceMeta-sequences are character sequences with special meanings in regular expression mode {n} {n ,}{ N, m} \ B \ D \ f \ n \ r \ s \ t \ unnnn \ v \ W \ xnn
1. {N} usage: match exactly n characters before (N is a non-negative integer.
VaR pattern: Regexp =/(Bug) {2}/the object contains two bugs consecutively. note that if var pattern: Regexp =/bug {2}/is used, it is Var STR: String = "bug"
VaR STR: String = "worms"
Trace (STR, "is valid:", pattern. Test (STR); Bug is valid: True.
2. {N,} usage: matches exactly n (n is a non-negative integer) or more characters before.
3. {N, m} usage: matches at least n characters and at most M characters.
4. \ B usage: matching the position between a word character and a non-word character can only be placed at the beginning and end. Nn, he does not support Chinese. It is still used in Chinese ^.
VaR pattern: Regexp =/\ B bug/
VaR pattern: Regexp =/. MP3 \ B/
VaR STR: String = "little worm" can be used to determine whether the file is in MP3 format.
Trace (STR, "is valid:", pattern.test(strter) 小 .mp3 is valid: True
5. \ B usage: the matching object must be within the boundary of the start and end of the target string. That is, the matching object cannot start with the target string, the end of the target string cannot be the opposite of \ B.
VaR pattern: Regexp =/\ B is/
VaR STR: String = "I Am a worm"
Trace (STR, "is valid:", pattern. Test (STR); I am a worm is valid: True
6. \ D usage: Used to match numbers from 0 to 9;
VaR pattern: the first character starting with Regexp =/\ B \ D/must be a number
VaR STR: String = "900/RMB"
Trace (STR, "is valid:", pattern. Test (STR); 900/RMB is valid: True
7. \ D usage: match any character except a number
VaR pattern: the first character starting with Regexp =/\ B \ D/must not be a number
VaR STR: String = "a900/RMB"
Trace (STR, "is valid:", pattern. Test (STR); a900/ is valid: True
8. \ F usage: concepts in the DOS operation age
9. \ N usage: Match line breaks
VaR pattern: Regexp =/\ n/
VaR STR: String = "welcome to the bug space! \ N"
Trace (STR, "is valid:", pattern. Test (STR ));
10. \ R usage: match the carriage return
VaR pattern: Regexp =/\ r/
VaR STR: String = "welcome to the bug space! \ R"
Trace (STR, "is valid:", pattern. Test (STR ));
11. \ S usage: match any blank characters (space, tab, line feed or carriage return)
VaR pattern: Regexp =/\ s/
VaR STR: String = "welcome to the bug space! "
Trace (STR, "is valid:", pattern. Test (STR); Welcome to the space of the worm! Is valid: True
12. \ S usage: match any character except white space
VaR pattern: Regexp =/\ s/
VaR STR: String = "welcome to the bug space! "
Trace (STR, "is valid:", pattern. Test (STR); Welcome to the space of the worm! Is valid: True
13. \ T usage: concepts in the Age of matching tab dos operations
14. \ Unnnn usage: the matching character code is a Unicode Character specified by the hexadecimal number NNNN. For example, \ u263a is a smiley character.
VaR pattern: Regexp =/\ u263a/
VaR STR: String = "welcome to the bug space! \ U263a"
Trace (STR, "is valid:", pattern. Test (STR); Welcome to the space of the worm! Is valid: True
15. \ V usage: Concept of matching Vertical page feed dos operation
16. \ W usage: match word characters (a-Z, a-Z, 0-9 or _). Note that \ W does not match non-English characters, such as é, N or C. It is different from.
VaR pattern: Regexp =/^ \ W/
VaR STR: String = "A Bug"
VaR STR: String = "0 bugs"
VaR STR: String = "A Bug"
VaR STR: String = "_ bug"
Trace (STR, "is valid:", pattern. Test (STR); _ bug is valid: True
17. \ W usage: match any character except word characters.
VaR pattern: Regexp =/^ \ W/
VaR STR: String = "worms"
Trace (STR, "is valid:", pattern. Test (STR); Bug is valid: True
18. \ Xnn usage: matches characters with the specified ASCII value (defined by the hexadecimal number NN.
VaR pattern: Regexp =/^ \ x41/; ""
VaR STR: String = "ABCD ";
Trace (STR, "is valid:", pattern. Test (STR); ABCD is valid: True
Flag: Flag can be accessed as a regular expression object attribute. Regular expressions have five signs: g I M S x
1. G usage: If G is not specified, only one is returned during search. For example:
VaR STR: String = "She sells seashells by the seashore .";
VaR pattern: Regexp =/sh \ W */;
Trace (Str. Match (pattern) Output: She
After specifying G:
VaR STR: String = "She sells seashells by the seashore .";
VaR pattern: Regexp =/sh \ W */g;
Trace (Str. Match (pattern) Output: She, shells, shore
2. I usage: by default, regular expression matching is case sensitive. If the I (ignorecase) flag is set, the Case sensitivity is ignored.
VaR STR: String = "She sells seashells by the seashore .";
VaR pattern: Regexp =/sh \ W */GI;
Trace (Str. Match (pattern) Output: She, shells, shore
3. M usage: matches the beginning and end of "line". Note that only the \ n character indicates the end of the line, but not the rest. Including \ r. For example:
VaR STR: String = "She sells seashells by the seashore. \ n ";
STR + = "she's your mother"
VaR pattern: Regexp =/^ sh \ W */Gim;
Trace (Str. Match (pattern) Output: She, she
It can also be used at the end. For example:
VaR STR: String = "She sells seashells by the seashore. \ n ";
STR + = "she's your mother. \ n"
VaR pattern: Regexp =/\ n \ $ \ W */Gim;
Trace ("=" + Str. Match (pattern) Output: =
4. S usage: Match line breaks with vertices
VaR STR: String = "<p> test \ n ";
STR + = "multiline </P> ";
VaR re: Regexp =/<p> .*? <\/P>/s;
Trace (Str. Match (re ));
5. X usage: When the X (extended) flag is used in a regular expression, all spaces typed in the mode are ignored.
VaR STR: String = "Lizhen ";
VaR re: Regexp =/Li Zhen/x
Trace (Str. Match (re); output: Lizhen
I have written some basic syntaxes of the regular expression. Basically, each has an example. It seems not difficult, but the difficulty of Regular Expressions lies in the combination of these basic elements. Some people think that reading regular expressions is like reading tianshu. There are a lot of symbols there, but after you have mastered his basics, you will be able to learn more and learn more.