Regular Expressions-PHP regular match content?

Source: Internet
Author: User
#sayhi   aaa   #牢骚    bbb     #test    ccc

Want to match out

#sayhi#牢骚#test

This label,
Then use:

preg_match_all("/#[a-zA-Z0-9\x{4e00}-\x{9fa5}]+<\\/span>/u", $s, $catspan);

The matching $catspan is

   Array ( [0] =>      Array (           #sayhi            #牢骚            #test             )          )       

What should I do next?

Reply content:

#sayhi   aaa   #牢骚    bbb     #test    ccc

Want to match out

#sayhi#牢骚#test

This label,
Then use:

preg_match_all("/#[a-zA-Z0-9\x{4e00}-\x{9fa5}]+<\\/span>/u", $s, $catspan);

The matching $catspan is

   Array ( [0] =>      Array (           #sayhi            #牢骚            #test             )          )       

What should I do next?

Preg_match_all ("/.*>#[a-zA-Z0-9x{4e00}-x{9fa5}]+<\/span>/u", $s, $catspan);

问题在这个.*上,正则默认是贪婪匹配,而.本身就代表所有字符,自然包括>在内,所以就把后面的内容都匹配进去了。
通过修改为.*?可以实现非贪婪匹配,这样就能正确结束于>上,获得你想要的结果。

 
$pattern = "/\]+\>[^\<]+\<\/span\>/";

 
preg_match_all('/
  
   
    
   ]+>#[^<]+<\\/span>/', $s, $catspan);
  
   

我的正则会这么写:

<>]*?>#[\s\S]*?

I'm not sure what you're writing about.

  • 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.