[Pin to the top] there is a wood that knows the syntax of nested matching in the regular expression

Source: Internet
Author: User
[Pin to the top] do you know the syntax of nested matching in a regular expression? Please explain it to me. baidu searched a lot and couldn't understand it. I hope the gods can explain what the syntax is like. thank you! Example: (a (B (d) c) (e (f (g) m )..... I want to get (a (B (d) c) & nbsp; and & nbsp; (e (f (g) c) [pin to the top] do you know the syntax of nested matching in a regular expression?
Please explain it to me. baidu searched a lot and couldn't understand it. I hope the gods can explain what the syntax is like. thank you!
For example:
(A (B (d) c) (e (f (g) m ).....
I want to get
(A (B (d) c) and (e (f (g) c) regular expressions nested matching share:
------ Solution --------------------
 $ Str = '(a (B (d) c) (e (f (g) m )';
$ Reg = '# \ ([^ \ (\)] +
------ Solution --------------------
(? R ))*\)#';

$ Matches = array ();
Preg_match_all ($ reg, $ str, $ matches );

Var_dump ($ matches );

------ Solution --------------------
Reference:
Quote: reference:

     $ Str = '(a (B (d) c) (e (f (g) m )';
$ Reg = '# \ ([^ \ (\)] +
------ Solution --------------------
(? R ))*\)#';

$ Matches = array ();
Preg_match_all ($ reg, $ str, $ matches );

Var_dump ($ matches );


Can you replace the brackets with html tags and then explain them?

You directly paste your actual code to help you analyze it.
------ Solution --------------------
$ Str = '';
// $ Reg = '# \ ([^ \ (\)] +
------ Solution --------------------
(? R ))*\)#';
// Match the content of the div with id 2 and id 35

$ Reg = '# ------ solution --------------------
35) "> (<([^> \ s] +) [^>] *> ([<>] *
------ Solution --------------------
(? 1 ))*? )*#';
Preg_match_all ($ reg, $ str, $ matches );
Echo"
";
var_dump($matches);
echo "
";

Although it can be used, it is not recommended to use this method. it is better to use tools such as DOMDocument to obtain content. after all, they are better at it.

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.