the 1th chapter first knowledge Regular expression
Regular Expression Definitions
- Regular expressions are a tool for searching and matching strings
application Examples of regular expressions
- Mobile input
- Windows File Search
- Linux List file commands
- Website User Registration
the 2nd Chapter analysis of the function of regular expression
Regular expression functions commonly used in PHP
- $pattern = Regular expression
- $subject = target function to match
- Preg_match ($pattern, $subject)
- Preg_match_all ($pattern, $subject, array & $matches)
Code:
$pattern = '/[0-9]/'; $subject = ' weur3ui76as83s0tk9 '; $m 1 = $m 2 = array (); $t 1 = preg_match ($pattern, $subject, $m 1); $t 2 = Preg_match_all ($pattern, $subject, $m 2); Show ($m 1); echo ' Show ($m 2); echo ' Show ($t 1. ' | | '. $t 2);
- preg_replace ($pattern, $replacement, $subject)
- Preg_filter ($pattern, $replacement, $subject)
Code:
$pattern = Array ('/[0123]/', '/[456]/', '/[789]/'); $subject = Array (' WEU ', ' r3ui ', ' 76as83 ', ' s ', ' 0tk9 '); $replacement = Array (' million ', ' thing ', ' house '); $str 1 = preg_replace ($pattern, $replacement, $subject); $str 2 = Preg_filter ($pattern, $replacement, $subject); Show ($str 1); echo ' Show ($str 2);
- Preg_grep ($pattern, array $input)
Code:
$pattern = '/[0-9]/'; $subject = Array (' WEU ', ' r3ui ', ' 76as83 ', ' s ', ' 0tk9 '); $arr = Preg_grep ($pattern, array $input); Show ($arr);
- Preg_split ($pattern, $subject)
Code:
$pattern = '/[0-9]/'; $subject = ' Silver 2 sang 3, 3 ' da ~ '; $arr = preg_split ($pattern, $subject); Show ($arr);
Code:
$str = ' qwer{asdf}[1234] '; $str = Preg_quote ($STR); Show ($STR);
PHP
regular-expression functions
Summary
- All start with Preg_.
- In addition to the Preg_quote function, the first parameter is a regular expression
- Preg_match-form inflammation, etc.
- Preg_match_all-illegal word filtering, etc.
3rd the basic syntax of regular expressions
delimiting characters
- Represents the start and end of a regular expression
- /[0-9]/
Regular Expression Tools
Atoms
- Visible atoms-characters that are visible to the naked eye with the keyboard output in a Unicode-encoded table
Punctuation " _ ? . ET-English alphanumeric A-Z, A-Z, 0-9-kanji, Japanese, Arabic and other languages-∑,∈,∮,≌, etc. physics formula symbols-other visible characters
- Invisible atoms-characters that are not visible to the naked eye after keyboard output in a Unicode encoding table
-line break \ n return \r-tab \t-Space-Other invisible symbols (Ps:unicode encoding conversion tool: Http://tool.oschina.net/encode)
Meta character
- | Matches two or more branch selections-[] matches any atom in square brackets-[^] matches any character except for atoms in square brackets
- . Match any character except newline-\d matches any decimal number, that is, [0-9]-\d matches any non-decimal number, that is, [^0-9]-\s matches an invisible atom, [\f\n\r\t\v]-\s matches a visible atom, i.e. [^\f\n\r\t\v] -\w matches any number, letter, or underscore, that is, [0-9a-za-z_]-\w matches any number, letter, or underscore, i.e. [^0-9a-za-z_]
quantifier
- {n} indicates that the preceding atom appears exactly n times
- {N,} indicates that the preceding atom appears at least n times
- {N,m} indicates that the preceding atom appears at least n times, with a maximum of M times
- * Match 0, 1, or more of its previous atoms, i.e. {0,}
- + match 1 or more times before the atom, i.e. {1,}
- ? Matches 0 or 1 times its previous atom, which is {0,1}
boundary Control and pattern Unit
- ^ matches where the string starts
- $ matches the position of the end of the string
- () match the whole of which is a single atom
<< Regular Expressions >> notes finishing