|
ru.perl- RU.PERL ---------------------------------------------------------------------- From : John Lepikhin 2:5070/156.1 22 Dec 2006 22:29:27 To : Sergey Subject : Re: Hет логики в re -------------------------------------------------------------------------------- ## Sergey wrote to Anton Yuzhaninov: S>>> Рассмотрим такую программку: S>>> $_='a'; s/.??//g; S>>> Кто угадает, что останется от строки? По логике, она не должна S>>> измениться. AY>> точка это любой символ, в том числе и a AY>> он будет найден и заменен на пустую строку. S> Так можно долго рассуждать, ты приведи алгоритм работы этого S> оператора. То, что написано в документациях и книжках, не S> соотвтествует результату работы. А для наглядности убери g и S> посмотри, будет ли найдена и заменена буква а. Это поведение вполне логично. Оно описано в perldoc perlop, раздел "Repeated patterns matching zero-length substring": --8<---------------cut here---------------start------------->8--- The higher level-loops preserve an additional state between iterations: whether the last match was zero-length. To break the loop, the follow- ing match after a zero-length match is prohibited to have a length of zero. This prohibition interacts with backtracking (see "Backtrack- ing"), and so the second best match is chosen if the best match is of zero length. For example: $_ = 'bar'; s/\w??/<$&>/g; results in "<><b><><a><><r><>". At each position of the string the best match given by non-greedy "??" is the zero-length match, and the second best match is what is matched by "\w". Thus zero-length matches alternate with one-character-long matches. Similarly, for repeated "m/()/g" the second-best match is the match at the position one notch further in the string. The additional state of being matched with zero-length is associated with the matched string, and is reset by each assignment to pos(). Zero-length matches at the end of the previous match are ignored during "split". --8<---------------cut here---------------end--------------->8--- -- mailto: john@{7!+30}.info. I am at home now. --- Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.19 (linux) * Origin: ISPsystem (2:5070/156.1) Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор
Архивное /ru.perl/330264ee3078.html, оценка из 5, голосов 10
|