Continue to ask questions about regular expressions

Source: Internet
Author: User
Continue to ask questions about regular expressions $ str & nbsp ;=& nbsp; "yesterday & nbsp ;@ & nbsp; hiaaaa & nbsp; dd @ Test & nbsp; "; $ reg & nbsp; = & nbsp; "/([^ @] + (? = \ S)/"; $ arr & nbsp ;=& nbsp; preg_split ($ reg, $ str,-1, PRE continue to ask about regular expressions

$ Str = "yesterday @ test hiaaaa dd @ Test reverse replay ";
$ Reg = "/([^ @] + (? = \ S ))/";
$ Arr = preg_split ($ reg, $ str,-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
Print_r ($ arr );


The result is
Array ([0] => yesterday [1] => @ [2] => test [3] => hiaaaa [4] => dd @ [5] => test [6] => repeat)


The expected result is

Array ([0] => yesterday @ [1] => test [2] => hiaaaa dd @ [3] => test [4] => repeat)


How can I modify the regular expression?

====================
Actually, I want to use a regular expression like the @ function in Weibo to split the @ user. The rule is "@ start + name + space". Is there any other better implementation method?

If you use this
$ Str = "yesterday @ test hiaaaa dd @ test @ 2 ";

In this way, I @ a friend at the end, but there is no space. Can I use regular expressions to determine? Regular expressions are shared:
------ Solution --------------------
$ Str = "yesterday @ test hiaaaa dd @ test @ 2 ";
$ Reg = "/(@ \ S + )/";
$ Arr = preg_split ($ reg, $ str,-1, PREG_SPLIT_DELIM_CAPTURE
------ Solution --------------------
PREG_SPLIT_NO_EMPTY );
Print_r ($ arr );
Array
(
[0] => yesterday
[1] => @ test
[2] => hiaaaa dd
[3] => @ test
[4] => repeat
[5] => @ Friend 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.