Please help me with regular expressions-php Tutorial

Source: Internet
Author: User
Please help me with regular expressions $ str = "111" <第一饭煲> | 9 <电饭煲海不复当年许多下挫> | 2212 <德国的花> | 1321 <不复电线饭煲杆分别当年> 424 | <苏当年丹公司订不复购饭煲> 5 | ";

$ Reg = "rice cooker ";
$ Reg1 = "no more ";
$ Reg2 = "current year ";

Preg_match_all ("/([0-9] +) <(. *". $ reg. ". *". $ reg1 .". *) >\|/isU ", $ str, $ arr );
Print_r ($ arr );

Please only match 1 out of the rice cooker Sea no longer that year many fell 2 no more than wire rice cooker pole respectively 3 Su that year Dan company no longer buy rice cooker


Reply to discussion (solution)

$ Str = "111
       <第一饭煲>
        
| 9
        <电饭煲海不复当年许多下挫>
         
| 2212
         <德国的花>
          
| 1321
          <不复电线饭煲杆分别当年>
           
424 |
           <苏当年丹公司订不复购饭煲>
            
5 |
            
              "; $ Reg =" "; $ reg1 =" no "; $ reg2 =" "; $ sp = explode ('|', $ str ); $ inArr = array (); foreach ($ sp as $ key => $ value) {if (strpos ($ value, $ reg )! = FALSE & strpos ($ value, $ reg1 )! = FALSE & strpos ($ value, $ reg2 )! = FALSE) {array_push ($ inArr, $ value) ;}} var_dump ($ inArr );
            
           
          
         
        
       

$ Str = "111
       <第一饭煲>
        
| 9
        <电饭煲海不复当年许多下挫>
         
| 2212
         <德国的花>
          
| 1321
          <不复电线饭煲杆分别当年>
           
424 |
           <苏当年丹公司订不复购饭煲>
            
5 |
            
              "; $ Reg =" "; $ reg1 =" no "; $ reg2 =" "; $ sp = explode ('|', $ str ); $ inArr = array (); foreach ($ sp as $ key => $ value) {if (strpos ($ value, $ reg )! = FALSE & strpos ($ value, $ reg1 )! = FALSE & strpos ($ value, $ reg2 )! = FALSE) {array_push ($ inArr, $ value) ;}} var_dump ($ inArr );
            
           
          
         
        
       

Is it regular? Because this text may be 1 MB or more, is it highly efficient to use arrays or regular expressions?
Which one is more efficient than regular expressions ?? If the number of arrays is 1 W? Text regular expression

$ S = "111"
       <第一饭煲>
        
| 9
        <电饭煲海不复当年许多下挫>
         
| 2212
         <德国的花>
          
| 1321
          <不复电线饭煲杆分别当年>
           
424 |
           <苏当年丹公司订不复购饭煲>
            
5 |
            
              "; Preg_match_all ('/(? <= <) ([^ |>] * (? :(? : Rice cooker | no longer | current year) [^ |>] *) {3} [^ |>] *)>/U', $ s, $ r ); print_r ($ r [1]);
            
           
          
         
        
       
Array
(
[0] => rice cookers no longer fall in the ocean.
[1] => no more than the current year
[2] => Sun Dan no longer buys rice cookers
)

$ S = "111"
       <第一饭煲>
        
| 9
        <电饭煲海不复当年许多下挫>
         
| 2212
         <德国的花>
          
| 1321
          <不复电线饭煲杆分别当年>
           
424 |
           <苏当年丹公司订不复购饭煲>
            
5 |
            
              "; Preg_match_all ('/(? <= <) ([^ |>] * (? :(? : Rice cooker | no longer | current year) [^ |>] *) {3} [^ |>] *)>/U', $ s, $ r ); print_r ($ r [1]);
            
           
          
         
        
       
Array
(
[0] => rice cookers no longer fall in the ocean.
[1] => no more than the current year
[2] => Sun Dan no longer buys rice cookers
)
Boss Xu, three identical defects can be eliminated.

Formal language, especially its own rules

$ S = "111"
       <第一饭煲>
        
| 9
        <电饭煲海不复当年许多下挫>
         
| 2212
         <德国的花>
          
| 1321
          <不复电线饭煲杆分别当年>
           
424 |
           <苏当年丹公司订不复购饭煲>
            
5 |
            
              "; $ A = explode ('|', $ s); $ d = array (" rice cooker "," no "," current year "); foreach ($ d as $ c) $ a = preg_grep ("/$ c/", $ a); print_r ($ );
            
           
          
         
        
       
Array
(
[1] => 9 <电饭煲海不复当年许多下挫>
[3] = & gt; 1321 <不复电线饭煲杆分别当年> 424
[4] => <苏当年丹公司订不复购饭煲> 5
)

$ S = "111"
          <第一饭煲>
           
| 9
           <电饭煲海不复当年许多下挫>
            
| 2212
            <德国的花>
             
| 1321
             <不复电线饭煲杆分别当年>
              
424 |
              <苏当年丹公司订不复购饭煲>
               
5 |
               
                 "; Preg_match_all ('/(? <= <) ([^ |>] * (? :(? : Rice cooker | no longer | current year) [^ |>] *) {3} [^ |>] *)>/U', $ s, $ r ); print_r ($ r [1]);
               
              
             
            
           
          
Array
(
[0] => rice cookers no longer fall in the ocean.
[1] => no more than the current year
[2] => Sun Dan no longer buys rice cookers
)
Boss, you need this regular expression, but now I want to extract the preceding number, because this is the data ID ....

Formal language, especially its own rules

$ S = "111"
          <第一饭煲>
           
| 9
           <电饭煲海不复当年许多下挫>
            
| 2212
            <德国的花>
             
| 1321
             <不复电线饭煲杆分别当年>
              
424 |
              <苏当年丹公司订不复购饭煲>
               
5 |
               
                 "; $ A = explode ('|', $ s); $ d = array (" rice cooker "," no "," current year "); foreach ($ d as $ c) $ a = preg_grep ("/$ c/", $ a); print_r ($ );
               
              
             
            
           
          
Array
(
[1] => 9 <电饭煲海不复当年许多下挫>
[3] = & gt; 1321 <不复电线饭煲杆分别当年> 424
[4] => <苏当年丹公司订不复购饭煲> 5
)
$ S = "111" <第一饭煲> | 9 <电饭煲海不复当年许多下挫> | 2212 <德国的花> | 1321 <不复电线饭煲杆分别当年> | 424 <苏当年丹公司订不复购饭煲> | 5 ";

Formal language, especially its own rules

$ S = "111"
                   <第一饭煲>
                    
| 9
                    <电饭煲海不复当年许多下挫>
                     
| 2212
                     <德国的花>
                      
| 1321
                      <不复电线饭煲杆分别当年>
                       
424 |
                       <苏当年丹公司订不复购饭煲>
                        
5 |
                        
                          "; $ A = explode ('|', $ s); $ d = array (" rice cooker "," no "," current year "); foreach ($ d as $ c) $ a = preg_grep ("/$ c/", $ a); print_r ($ );
                        
                       
                      
                     
                    
                   
Array
(
[1] => 9 <电饭煲海不复当年许多下挫>
[3] = & gt; 1321 <不复电线饭煲杆分别当年> 424
[4] => <苏当年丹公司订不复购饭煲> 5
)
Boss, your regular expression is too high-end. after a long time, I finally figured it out.
But not next time.

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.