[英]php regular expression plus sign
I'm playing around with PHP Regex in order to improve my skills with it. 我正在玩PHP Regex,以提高我的技能。
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
. 在正则表达式中, +
表示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. 模式/(\\w)/
表示在单个组中匹配单个单词字符( a-zA-Z0-9_
)。 So it will match each letter. 因此它将匹配每个字母。 The first match group will be just a
. 第一个比赛组将只是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. 模式/(\\w+)/
将匹配一组中的一个或多个单词字符。 So it will match each block of letters. 因此它将匹配每个字母块。 The first match group will be aaa
. 第一个比赛组将为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. 最后一个模式/(\\w)+/
比较棘手,但是将匹配组中的单个单词字符,但是+
表示将匹配组中的一个或多个字符。 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). 所以,第一场比赛将是a
,但替换将替换所有群体的,直到有没有与最后匹配的组(当然跟一个星号)匹配。 So if you tried the string aaab ccc
, your result would end up as b* c*
. 因此,如果您尝试使用字符串aaab ccc
,则结果将最终为b* c*
。 b
is the last matched group in the first sequence and so the replace would use that. b
是第一个序列中的最后一个匹配组,因此替换将使用该组。
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... 括号用于将您的匹配项分组为一个变量,因此为什么可以执行$ 1,第二组()给您$ 2,依此类推...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.