php positive atoms is shown in Expression

 

atom

Atom is the smallest unit in which the regular expression shown, it means the content of atoms to be matched. Establishment of a regular expression which must be shown to have a minimum of one atom. Marble platform accuracy class

All visible atom is invisible characters

Description: We see space, carriage return, line feed, 0-9, A-Za-z, Chinese, punctuation marks, special symbols are all atoms.

Before doing instance atom of us first explain a function, preg_match:

int preg_match (string $ regular, string $ string [, array & $ result])

Function: $ canonical variables, matching $ string variable. If there is the number of matches is returned, the result of the match to put $ result variable. If no match result is returned to zero.

Note: The above is mainly used preg_match several parameters. I will be on top of several other parameters not listed. Because the other two parameters too common a.

We come to prove by experiment:

<?php
// define a variable called zz, put the regular expression is shown. In order to facilitate memory, if you compare the English ok, I suggested that the variable name is written in English $ pattern.
$zz = '/a/';

$string = 'ddfdjjvi2jfvkwkfi24';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

Because I want is a match, while $ string when it is a non-existent, so unsuccessful.

<?php
$zz = '/wq/';

$string = 'ssssswqaaaaaa';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

S presence wq above string, thus the matching is successful.

Next we try to match a space:

<?php
$zz = '/ /';

$string = 'sssssw aaaaa';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

Execution results are as follows:

QQ screenshot 20161114135142.png

Because on the $ string variable w character exists a space. Therefore, the matching is successful, outputs a character string type, length is 1. But we are not visible to the naked eye can not see this string only.

Special identifier atoms

atom Explanation
\d A 0-9 match
\D In addition to all the characters 0-9
\w a-zA-Z0-9_
\W All characters except 0-9A-Za-z_ of
\s Matches all whitespace characters \ n \ t \ r spaces
\S All matches non-whitespace characters
[ ] Atoms specified range


This one needs to remember, to reach the best level of dictation. Remember when paired memory, \ d is a match 0-9, then \ D 0-9 is in addition to all the characters.
As already explained very clearly, step by step, we conducted experiments to learn these.

When you learn, for sure the atoms reach the level of dictation. Because we do after the experiment time, little by little you learn.

\ D matches a 0-9

<?php
$zz = '/\d/';

$ String = 'Do you love to drink I drink 9';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

\ D matches a value of 0-9 non

<?php
$zz = '/\D/';

$ String = '121243 in 23453453';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

A successful match, the match into. It is not because the character between 0-9.

\ W matches a a-zA-Z0-9_

<?php
$ Zz = '/ \ w /';

$ String = '_ new country Viva Viva Yes';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

Matches, match to underline.

\ W matches a non-a-zA-Z0-9_

<?php
$ Zz = '/ \ w /';

$string = 'afasABCWEQR44231284737';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

Match fails. Because the above is above all a-zA-Z0-9_, no non-a-zA-Z0-9_.

\ S matches any whitespace character \ n \ t \ r spaces

<?php
$ Zz = '/ \ s /';

$ String = "China Wan
year old";

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

The match is successful, because there is a carriage return.

\ S non-blank character

<?php
$ Zz = '/ \ s /';

$string = "        
         a       ";

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

A successful match. Although there are spaces, carriage returns and indent above. However, there is a non-blank character a. Accordingly, the matching success.

[] Atoms in the specified range

<?php

$ Zz = '/ [0-5] \ w + /';

$string = '6a';

$string1 = '1C';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

Conclusion:
Example 0-5 match $ string failure, and $ string1 success. Because the first value of $ string is 6, is not in [0-5] in the range of.

<?php

$ Zz = "/ [a-zA-Z0-9 _] \ w / ';

$string = 'ab';

$string1 = '9A';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

in conclusion:

$ String and $ string1 match success. Since \ w is [a-zA-Z0-9_]

<?php

$zz = '/[abc]\d+/';

$ String = 'a9';

$string1 = 'b1';

$string2 = 'c5';

$string3 = 'd4';


if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

in conclusion:

$ String, $ string1, $ string2 match is successful, and $ string3 unsuccessful. Because more than $ string3 [abc] range, which is from the beginning of d.

[^ Characters] does not match the specified range of characters

<?php

$ Zz = '/ [^ 0-9a-Za-z _] /';

$string = 'aaaaab311dd';

$string1 = '!$@!#%$#^##';

if(preg_match($zz, $string, $matches)){
   echo 'matched, the result is:';
   var_dump ($ matches);
}else{
   echo 'not matched';
}

?>

in conclusion:

    1. $ string matching is not successful, but the success when the matching $ string1. Since the brackets which has a circumflex.

    2. ^ circumflex role is not allowed inside the brackets in order to match characters in brackets.

to sum up:

atom Equivalence formula
\w [A-zA-Z0-9_]
\W [^ A-zA-Z0-9_]
\d [0-9]
\D [^0-9]
\s [ \t\n\f\r]
\S [^ \t\n\f\r]

Guess you like

Origin www.cnblogs.com/furuihua/p/12073155.html
Recommended