Home  >  Q&A  >  body text

$zz = '/[0-5]\w+/'; What is the + sign used for? Why is it not included in some of the examples below?

<?php


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



##$string = '1C';

$string2 = '1C$';


##if(preg_match ($zz, $string, $matches)){

echo 'Matched, the result is:';

var_dump($matches);

}else{

echo 'No match';

}

##?>

Q1:$zz = '/ [0-5]\w /'; What is this number used for? Why is it not included in some examples later?

Q2: Why is the $string output result 1C? Shouldn't it be "1"?

Q3: If the output result of $string is 1C, shouldn’t the output result of $string1 be “1C$”?

益伦益伦2529 days ago1529

reply all(3)I'll reply

  • 路过

    路过2017-10-21 22:24:59

    [0-5] can match any one of 0-5

    \w matches any word character including underscores. Equivalent to '[A-Za-z0-9_]'. $ is not in scope

    + identifies one or more times

    reply
    0
  • 寻觅 beyond

    寻觅 beyond2017-10-21 12:35:29

    Because $, +, \, etc. are all special symbols, so when you want to match these characters, you must clearly indicate which special symbol you want to match in $pattern (use a backslash to transfer),

    For example If you want to match $ in $string2, just change $zz = '/[0-5]\w+/'; to $zz = '/[0-5]\w+\$/';

    reply
    0
  • 寻觅 beyond

    寻觅 beyond2017-10-21 12:27:02

    Q1, + means matching the previous atom appearing 1 or more times

    Q2, see Q1, + means the previous \w (character) appears 1 or more times, so the C after 1 also Will be matched

    Q3, $, +, - and other symbols are special symbols. I forgot where I read a blog. \w cannot match these special symbols. You can try changing $ to Other letters can be matched. As for the reason, if anyone passes by, I hope to explain it

    reply
    0
  • Cancelreply