How to deal with the regular expression of the substring

Source: Internet
Author: User
Tags preg
The regular expression used to obtain substrings has the following strings: & quot; & lt; str1 & gt; name1 & lt;/str1 & gt; & lt; str2 & gt; name2 & lt; /str2 & gt; & quot; how do I retrieve & quot; name1 & quot; and & quot; name2 & quot; using regular expressions? Thank you. use PHPcode to obtain the regular expression of the substring.
There are the following strings:
" Name1 Name2 "
How can I retrieve "name1" and "name2" using regular expressions? Thank you.

------ Solution --------------------
PHP code
  Name1
  
   
Name2
  "; $ Patten = "/
  
   
([^ <] *) <\/Str \ d +>/isU "; preg_match_all ($ patten, $ str, $ matches); print_r ($ matches [1]);
   
------ Solution --------------------
PHP code
$ Str ="
     
      
Name1
     
     
      
Name2
     "; Preg_match_all ('/> ([^ <] +)Name1 [1] => name2 )*/
------ Solution --------------------
PHP code
Preg_match_all ("/> [^ <] +
       ------ Solution --------------------
$ Str =" Name1 Name2 ";
Preg_match_all ('/> ([^ <] +)Print_r ($ matches [1]);
/**
Output result:
Array ([0] => name1 [1] => name2)
*/

------ Solution --------------------
PHP code
         Name1
         
          
Name2
         "; $ Preg = "#
         
          
(.*)
         
         
          
(.*)
         # "; Preg_match ($ preg, $ str, $ arr); echo $ arr [1]; echo $ arr [2];?>
------ Solution --------------------
$ Str =" Name1 Name2 ";
Preg_match_all ("/<[a-z0-9] *> ([^ <|>] *) <\/[a-z0-9] *>/", $ str, $ matches );
Echo $ matches [1] [0];
Echo $ matches [1] [1];
?>

------ Solution --------------------
"/> (.*?) <\//"
------ Solution --------------------
Discussion

PHP code

$ Str =" Name1 Name2 ";
Preg_match_all ('/> ([^ <] +)Print_r ($ matches [1]);
/**
Output result:
Array ([0] => name1 [1] => name2)
*/

------ Solution --------------------
$ Str =" Name1 Name2 ";
Preg_match_all ("/<[a-z0-9] *> ([^ <|>] *) <\/[a-z0-9] *>/", $ str, $ matches );
Echo $ matches [1] [0];
Echo $ matches [1] [1];
?>

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.