Php uses regular expressions to extract strings in the brackets, Parentheses, brackets, and braces.

Source: Internet
Author: User
PHP uses regular expressions to extract character examples in string brackets, Parentheses (), brackets [], and braces {}. For more information, see

PHP uses regular expressions to extract character examples in string brackets, Parentheses (), brackets [], and braces {}. For more information, see

The Code is as follows:


$ Str = "Hello <我> (Love) [BEIJING] {Tiananmen Square }";

Echo f1 ($ str); // return hello
Echo f2 ($ str); // return me
Echo f3 ($ str); // returns love
Echo f4 ($ str); // return to Beijing
Echo f5 ($ str); // return to Tiananmen

Function f1 ($ str)
{
$ Result = array ();
Preg_match_all ("/^ (.*)(? : <)/I ", $ str, $ result );
Return $ result [1] [0];
}

Function f2 ($ str)
{
$ Result = array ();
Preg_match_all ("/(? : <)(.*)(? :>)/I ", $ str, $ result );
Return $ result [1] [0];
}

Function f3 ($ str)
{
$ Result = array ();
Preg_match_all ("/(? :\()(.*)(? : \)/I ", $ str, $ result );
Return $ result [1] [0];
}

Function f4 ($ str)
{
$ Result = array ();
Preg_match_all ("/(? :\[)(.*)(? : \])/I ", $ str, $ result );
Return $ result [1] [0];
}

Function f5 ($ str)
{
$ Result = array ();
Preg_match_all ("/(? :\{)(.*)(? :\})/I ", $ str, $ result );
Return $ result [1] [0];
}

PS :(? : Character) indicates that this character is not captured. It seems that PHP does not support replacing characters with parentheses.
Otherwise, you can view the loop as nested, and then perform loop matching.
PS2: Huanshi :(?!) (? = )(? If there is a smaller number, match on the right, and if not, match on the left. Exclamation points indicate unequal, and equal signs indicate equal.
PS3: all of them have been verified by the validators. For more information about the validators, see references.

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.