search

Home  >  Q&A  >  body text

Regular expression* problem

<?php

$zz = '/Audi*/';

##$string = "Audi 2500 is my favorite Love";

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

echo 'Matched, the result is: ';

var_dump($matches);

}else{

echo 'No match';

}

? >

Why is the output result 'Audi' instead of 'Audi Audi' or 'Audi Di'? Isn't * at least 0 times? How come not all of them were extracted in two cases?

益伦益伦2580 days ago1535

reply all(8)I'll reply

  • 又白又帅又可爱

    又白又帅又可爱2017-11-20 12:15:33

    Using preg_match should match "Audi", using preg_match_all should match "Audi Audi",

    This is a theoretical result, but in fact I have not seen any examples of regular matching in Chinese. Regular It can only check whether it is Chinese and has a limited length, and it uses Chinese bytecode, similar to this: u4e00-u9fa5

    reply
    0
  • 益伦

    Okay, I tried a lot of Chinese and it really didn’t work. I will use Chinese less as examples in the future.

    益伦 · 2017-11-20 23:17:33
  • 路过

    路过2017-11-18 10:27:36

    preg_match only matches one

    preg_match_all is the entire matching result

    It is recommended to check the manual

    reply
    0
  • 益伦

    <?php $zz = '/250*/'; $string = "Dior and Audi 25000 are my favorites"; if(preg_match($zz, $string, $matches)){ echo 'Matched, the result is:'; var_dump($matches); }else{ echo 'no match'; } ?> It’s getting more and more confusing, so why is the result of this code 25000, not 250 as you said?

    益伦 · 2017-11-18 11:01:48
    益伦

    Match the character before it any number of times (0 or any number of times)

    益伦 · 2017-11-18 11:08:10
    益伦

    Okay, thank you for answering

    益伦 · 2017-11-20 23:12:45
    路过

    Do you know what * means?

    路过 · 2017-11-18 11:03:33
    路过

    Just read the manual

    路过 · 2017-11-18 11:39:39
  • Cancelreply