Why is this matching wrong? what should I do if the regular expression is difficult?

Source: Internet
Author: User
Why is this matching incorrect? what is the difficulty of regular expression ;? Php $ txt = & quot; [face = 'simhei '] 12345 [/face] [face = 'simhei'] haha [/face] & quot ;; $ return = preg_match_all (& quot;/\ [face = '(. *) '] (why is the matching incorrect? it is a difficult regular expression.

$ Txt = "[face = 'simhei '] 12345 [/face] [face = 'simhei'] haha [/face]";
$ Return = preg_match_all ("/\ [face = '(. *)'] (. + ?) \ [\/Face]/", $ txt, $ tar );
Echo"
";
Print_r ($ tar );
?>

My match means the shortest string ending with [/face] with [face = 'arbitrary number...

Where is the error ???

Any number seems to be excluded [/face]

Array
(
[0] => Array
(
[0] => [face = 'simhei '] 12345 [/face] [face = 'simhei'] haha [/face]
)

[1] => Array
(
[0] => simhei '] 12345 [/face] [face = 'simhei
)

[2] => Array
(
[0] => haha
)

)

The result is incorrect. the longest matched string is dizzy.

------ Solution --------------------
...... /\ [Face = '(. *)']… => ...... /\ [Face = '(.*?) ']……

No ?, Is greedy, and the result is as you paste
------ Solution --------------------
/\ [Face = '(. *)'] (. + ?) \ [\/Face]/U can be added with a U.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.