[英]PHP regex Pattern Modifiers A and D
Can anyone help me with modifiers A
and D
? 有人可以用修饰符
A
和D
帮助我吗?
I read the description 3 times and did a couple of tests on regex101 but I can not do it so that they would work. 我阅读了3次描述,并在regex101上进行了一些测试,但我无法做到这一点,以便它们可以正常工作。 Or I can not find an example of what they would have earned.
否则我找不到他们将获得的收入的例子。
For example, the regular expression 例如正则表达式
<u>[a-z]+<\/u>
works the same way with A
and without A
以同样的方式有
A
,没有A
https://regex101.com/r/X3nkMF/1/ https://regex101.com/r/X3nkMF/1/
See PHP/PCRE Manual: Possible modifiers in regex patterns 参见PHP / PCRE手册:正则表达式模式中的可能修饰符
A
(PCRE_ANCHORED)
A
(PCRE_ANCHORED)
If this modifier is set, the pattern is forced to be " anchored ", that is, it is constrained to match only at the start of the string which is being searched (the "subject string").
如果设置了此修饰符,则将模式强制为“ 锚定 ”,即, 仅在要搜索的字符串 (“主题字符串”) 的开头将其约束为匹配 。 This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
也可以通过模式本身中的适当构造来实现此效果,这是在Perl中实现此效果的唯一方法。
Example : /bar/A
matches bar baz
but not foo bar
示例 :
/bar/A
匹配bar baz
但不 匹配 foo bar
There is also the \\A
anchor available to match start of the string. \\A
锚可用于匹配字符串的开头。 This is helpful in multiline mode (using the m
flag ) where ^
matches start of each line. 这在多行模式(使用
m
标志 )中很有用,其中^
匹配每行的开始。
D
(PCRE_DOLLAR_ENDONLY)
D
(PCRE_DOLLAR_ENDONLY)
If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string.
如果设置了此修饰符,则模式中的美元元字符仅在主题字符串的末尾匹配。 Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines).
如果没有此修饰符,则如果美元是换行符,则美元也会紧接在最后一个字符之前匹配 (但不匹配其他任何换行符)。 This modifier is ignored if m modifier is set.
如果设置了m修饰符,则忽略该修饰符。 There is no equivalent to this modifier in Perl.
在Perl中没有与此修饰符等效的东西。
Example : /foo$/D
matches foo
but not foo\\n
示例 :
/foo$/D
匹配foo
但不 匹配 foo\\n
There is also the lower \\z
anchor available to match the absolute end of the string: foo\\z
Whereas the upper \\Z
would behave similar the dollar sign and also match before last \\n
with the difference that in multiline mode ( m
flag) upper \\Z
won't match at the end of each line. 还有一个较低的
\\z
定位符可用来匹配字符串的绝对结尾: foo\\z
而较高的\\Z
表现类似于美元符号,并且也匹配最后一个\\n
,区别在于多行模式( m
标志)上\\Z
在每一行的末尾将不匹配。
<u>[az]+<\\/u>
It does not matter whether you anchor that pattern to the beginning or not, it will always match the first line of 不论是否将该模式锚定到开始都无关紧要,它将始终与该模式的第一行匹配
<u>word</u> <u>main</u>
only - unless you add the g
modifier to not stop after the first match. 仅-除非您添加
g
修饰符以在第一个匹配项后不停止。
So add /g
and /gA
, and then you will see what a difference this A
makes ... 因此,添加
/g
和/gA
,然后您将看到A
什么不同...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.