Simple Introduction to Regular expressions
Regular expressions have a lot of application in normal programming, and for anyone who wants to learn programming, regular expressions are a must-have knowledge.
Not much nonsense to say, the following first to do a simple introduction to the regular expression, in the subsequent articles, will be detailed introduction.
One, meta-character
Metacharacters a total of 12: $ () [{? + *. ^ \ |
Metacharacters has a special meaning, and if you want to use its literal value, you must escape it.
such as: \$ \* \ (et cetera )
two, control characters or non-printable characters
\a Alarm
\e exit
\f Page Change
\ nthe line break
\ r Enter
\ t Horizontal Tabulation
\v Vertical Tabulation
Third, shorthand
\d single digit [0-9] ( Note: [] means one of all the characters in the brackets;-is a concatenated character, representing all characters greater than or equal to 0 less than or equal to 9 )
\w letters, numbers, underscores
\s whitespace characters, including spaces, line breaks, tabs
Note: \d \w \s matches characters that are not matched by \d \w \s
Iv. Repetition
Fixed number of times: {8}---repeated 8 times
Infinite times: {n}
0 or more times: {0,} equivalent to *
One or more times: {1,} equivalent to +
0 or one time: {0,1} equivalent to?
Let's look at an example:
The matching time format requires the following:
1. The hours and seconds are two digits, and the milliseconds are represented by three digits.
2. The corresponding range: 00--99
3. The range of minute and second corresponds: 00--59
4. Milliseconds corresponding to the range: 000--999
5. Format as follows: 12:34:56.789
Answer:
1, when the two digits are 0 to 9 one of them, we can be expressed as [0-9][0-9] or [0-9]{2} or \d\d or \d{2}
2, minute and second format is the same, and it is preceded by:, so we represent (: [0-5]\d) {2}
3, milliseconds can be expressed as \d\d\d or \d{3}
Total: \d\d (: [0-5]\d) {2}\.\d{3} note there is a meta-character ".", we take its literal value and need to be escaped.
Verify with Regexbuddy:
Simple Introduction to Regular expressions