Подтвердить что ты не робот

Регулярные выражения: разность между ^ и\A

Разница между ^ и \A заключается в том, что \A никогда не может совпадать после разрыва строки? (даже в многострочном режиме)

The PCRE man page says:
^      assert start of string (or line, in multiline mode)
...
\A     matches at the start of the subject

Спасибо!

4b9b3361

Ответ 1

Да. \A будет соответствовать в самом начале вашего значения. ^ будет соответствовать началу значения, но также будет совпадать сразу после новых строк в многострочном режиме (//m).

\Z похож, но с окончанием значения. Тем не менее, он также будет соответствовать перед новой строкой в ​​конце значения. Если вы не хотите этого поведения, используйте \Z, который соответствует только в конце значения.

Полезная ссылка: perlre manpage

Ответ 2

Если у вас это в качестве цели или темы:

Line 1\n
Line 2\n
Line 3\n

Регулярное выражение /^Line/gm будет соответствовать всем трем строкам. Якорь ^ совпадает с первой частью строки или после логического CR/LF, если присутствует флаг /m.

Регулярное выражение /\ALine/gm будет соответствовать только первой строке независимо от того, что. Утверждение \A соответствует только в абсолютном начале целевой или предметной строки независимо от флага /m.

Ответ 3

Да, согласно документации:

Утверждения\A,\Z и \z отличаются от традиционных обходных и доллар (описанный в следующем разделе) в том, что они только совпадают в самом начале и конце строки темы, любые параметры набор.

Ответ 4

Если вы читаете perldoc perlretut, это выдержка, полезная для вашего понимания.

   ·   s modifier (//s): Treat string as a single long line.  '.' matches any character, even "\n".  "^" matches only at the beginning of the string
       and "$" matches only at the end or before a newline at the end.

   ·   m modifier (//m): Treat string as a set of multiple lines.  '.' matches any character except "\n".  "^" and "$" are able to match at the start
       or end of any line within the string.

   ·   both s and m modifiers (//sm): Treat string as a single long line, but detect multiple lines.  '.' matches any character, even "\n".  "^" and
       "$", however, are able to match at the start or end of any line within the string.

   Here are examples of "//s" and "//m" in action:

       $x = "There once was a girl\nWho programmed in Perl\n";

       $x =~ /^Who/;   # doesn't match, "Who" not at start of string
       $x =~ /^Who/s;  # doesn't match, "Who" not at start of string
       $x =~ /^Who/m;  # matches, "Who" at start of second line
       $x =~ /^Who/sm; # matches, "Who" at start of second line

       $x =~ /girl.Who/;   # doesn't match, "." doesn't match "\n"
       $x =~ /girl.Who/s;  # matches, "." matches "\n"
       $x =~ /girl.Who/m;  # doesn't match, "." doesn't match "\n"
       $x =~ /girl.Who/sm; # matches, "." matches "\n"

   Most of the time, the default behavior is what is wanted, but "//s" and "//m" are occasionally very useful.  If "//m" is being used, the start of
   the string can still be matched with "\A" and the end of the string can still be matched with the anchors "\Z" (matches both the end and the
   newline before, like "$"), and "\z" (matches only the end):

       $x =~ /^Who/m;   # matches, "Who" at start of second line
       $x =~ /\AWho/m;  # doesn't match, "Who" is not at start of string

       $x =~ /girl$/m;  # matches, "girl" at end of first line
       $x =~ /girl\Z/m; # doesn't match, "girl" is not at end of string

       $x =~ /Perl\Z/m; # matches, "Perl" is at newline before end
       $x =~ /Perl\z/m; # doesn't match, "Perl" is not at end of string