Trong perlretut nó nói rằng bạn có thể backreference trong một regex (không phải là phần bên phải của quyền thay người) sử dụng \g1
. Điều này đã được thay đổi trong 5,14. Vì tôi chỉ có 5.12.2 ở đây, tôi phải sử dụng \1
để thay thế.
Do đó, regex ban đầu của bạn với một adjustion nhỏ làm việc cho tôi:
use strict; use warnings;
use 5.12.2;
use feature qw(say);
for (qw/ azalea baobab cyclic deadend teeeeeestest doesnotwork /) {
say if m/^([a-z])[^\1]+\1[^\1]+\1$/i;
}
Nhìn vào nó với YAPE::Regex::Explain
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();
sản lượng:
The regular expression:
(?i-msx:^([a-z])[^\1]+\1[^\1]+\1$)
matches as follows:
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();
NODE EXPLANATION
----------------------------------------------------------------------
(?i-msx: group, but do not capture (case-insensitive)
(with^and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[^\1]+ any character except: '\1' (1 or more
times (matching the most amount possible))
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
[^\1]+ any character except: '\1' (1 or more
times (matching the most amount possible))
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
Sửa: Địa chỉ một lớp lót là perl -e 'print if m/^([a-z])[^\1]+\1[^\1]+\1$/i'
.
Ngày lưu ý khác, nếu bạn đã cố gắng perl -w -e 'print if m/(as)$1/'
bạn muốn đã thấy vấn đề của bạn ngay lập tức:
$ perl -w -e 'print if m/(a)$1/' asdf
Use of uninitialized value $1 in regexp compilation at -e line 1.
Use of uninitialized value $_ in pattern match (m//) at -e line 1.
Những gì tôi đã không tìm ra lý do tại sao là nó phù hợp với ololololo
.