2010-10-26 8 views
4

La seule différence entre ^ et \A est le fait que \A ne peut jamais correspondre après un saut de ligne? (même en mode multiligne)Expressions régulières: différence entre^et A

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

Merci!

Répondre

8

Oui. \A correspondra au tout début de votre valeur. ^ correspond au début de la valeur, mais correspondra également immédiatement après les nouvelles lignes en mode multiligne (//m). Le \Z est similaire, mais avec la fin de la valeur. Cependant, il sera également correspondre avant un retour à la ligne à la fin de la valeur. Si vous ne souhaitez pas ce comportement, utilisez \z, qui correspond à uniquement à la fin de la valeur.

Référence utile: perlre manpage

+0

Cette réponse a été ajouté à la [Stack Overflow Expression régulière FAQ] (http://stackoverflow.com/a/22944075/2736496), sous la rubrique "ancres". – aliteralmind

2

Oui, selon le documentation:

Le \ A, \ Z, et \ assertions z diffèrent de la circonflexe traditionnelle et le dollar (décrit dans la section suivante) en ce sens qu'ils ne jamais égaler au tout début et à la fin de la chaîne de caractères, toutes les options sont .

4

Si vous avez ce que votre cible ou une chaîne sujet:

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

Le regex /^Line/gm correspond à tous les trois lignes. L'ancre ^ correspond à la première partie de la chaîne ou après un CR/LF logique si le drapeau /m est présent.

La regex /\ALine/gm ne correspondra à la première ligne, quoi qu'il arrive. L'assertion \A correspond uniquement au début absolu de la chaîne cible ou de l'objet, quel que soit l'indicateur /m.

0

Si vous lisez perldoc perlretut, ceci est un extrait qui est utile à votre compréhension.

· 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