简体   繁体   中英

php regular expression plus sign

I'm playing around with PHP Regex in order to improve my skills with it.

I'm having a hard time trying to understand the plus sign - so I wrote the following code:

$subject = 'aaa bbb cccc dddd';
echo preg_replace('/(\w)/',"$1*",$subject) . '<br>';
echo preg_replace('/(\w+)/',"$1*",$subject) . '<br>';
echo preg_replace('/(\w)+/',"$1*",$subject) . '<br>';

With results in:

a*a*a* b*b*b* c*c*c*c* d*d*d*d*
aaa* bbb* cccc* dddd*
a* b* c* d*

I don't understand why these results come about. Can someone please explain what's going on in this example

in regular expressions, + means one or more of the preceding character or group .

The pattern /(\\w)/ , means match a single word character ( a-zA-Z0-9_ ) in a single group. So it will match each letter. The first match group will be just a . The replace will replace each individual letter with that letter followed by an asterisk.

The pattern /(\\w+)/ will match one or more word characters in a group. So it will match each block of letters. The first match group will be aaa . The replace will replace each block of multiple letters followed by a asterisk.

The last pattern /(\\w)+/ is a little more tricky, but will match a single word character in a group but the + means that it will match one or more of the group. So the first match will be a , but the replace will replace all of the groups until there isn't a match with the last matched group (of course followed by an asterisk). So if you tried the string aaab ccc , your result would end up as b* c* . b is the last matched group in the first sequence and so the replace would use that.

Your mistake isn't the plus sign, it's understanding what the parentesis is for and how it works. The parenthesis is for grouping your match into a variable, hence why you can do $1, the second set of () gives you $2 and so on...

  • (\\w) means 1 word character
  • (\\w+) means 1 or more word characters
  • (\\w)+ matches 1 or more word characters, but only the first one is put into the variable, because only the \\w is inside the paranthesis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM