Ask how to match a regular expression.

Source: Internet
Author: User
Ask how to match a regular expression:

(.*)



Content:
  • Str
  • Title 1
  • Title 2



    Target: (must contain

    )

    Title 2




    Current error result: (The entire segment is matched)
    Title 1

    Title 2


    Reply to discussion (solution)

    Add U to prohibit greed

    Is that what it means?

    $ Str ='
  • Str
  • Title 1
  • Title 2

    '; Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/is ", $ str, $ match); echo"

    ";print_r($match);

    Is that what it means?

    $ Str ='
  • Str
  • Title 1
  • Title 2

    '; Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/is ", $ str, $ match); echo"

    ";print_r($match);



    Correct results:

    Title 2



    Current error result:
    Title 1

    Title 2



    I tried this and matched the following results.

    Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/isU ", $ str, $ match); // add U to prohibit greedy mode.

    Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/isU ", $ str, $ match); // add U to prohibit greedy mode.


    Damn csdn
    Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/is U ", $ str, $ match );

    Add U to prohibit greed
    It seems that it is not greedy because it immediately ends after matching the results.

    Added U

    $ Str ='
  • Str
  • Title 1
  • Title 2

    '; Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/U ", $ str, $ match); echo"

    ";print_r($match);


    Result:
    Array ([0] => Array ([0] => title 1

    Title 2

    ) [1] => Array ([0] => title 2 ))


    Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/isU ", $ str, $ match); // add U to prohibit greedy mode.


    Damn csdn
    Preg_match_all ("/

    (. *) <\/B> <\/p> <\/a>/is U ", $ str, $ match );



    Result:

    Array ([0] => Array ([0] => title 1

    Title 2

    ) [1] => Array ([0] => title 2 ))


    The previous pile of this is redundant:
    Title 1

    Give a reminder
    To match some boundary content, write regular expressions in the form of boundary + exclusive boundary + boundary to avoid random use of dots.
    In particular, html and other markup languages. because the markup language is characterized by a tag, it is also legal to include a line break in the middle, and the dot cannot be covered. if you use a dot, it is a bit of an assumption that the source string must be neat.
    For example:
    Href ="
    Abc/abc.html ">
    ABC

    This html string is completely legal and does not affect the display of the webpage at all, but the regular expression is required.

    "[^"] *"
    <[^>] +>
    [^ <] *

    $ S = <TXT
  • Str
  • Title 1
  • Title 2

    TXT; preg_match ('/. + ()/is', $ s, $ m); echo $ m [1];

    Title 2

    Simplify it.

    Preg_match ('/. + ()/is', $ s, $ m); echo $ m [1];/*

    Title 2

    */

    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.