close
Regex當中的lookaround技巧是較少見到,會用卻是很有用,以下以Perl說明之:
my @nth = qw/1st 2nd 3rd 4th 5th 6th nth/; 被比較的內容1
my @nam = qw/jeffery jef jeff jeffs jefferson jefson/; 被比較的內容2
my @nam = qw/jeffery jef jeff jeffs jefferson jefson/; 被比較的內容2
my @pattern_nth = qw/
(?=\dth)\d
(?!\dth)\d
(?<=\d)th
(?<!\d)th
/;
(?=\dth)\d
(?!\dth)\d
(?<=\d)th
(?<!\d)th
/;
foreach $pattern (@pattern_nth) {
print "$pattern : ";
foreach (@nth) {
print "$_ " if (/$pattern/);
}
print "\n";
}
print "$pattern : ";
foreach (@nth) {
print "$_ " if (/$pattern/);
}
print "\n";
}
my @pattern_nam = qw/
(?=jeffer)jef
(?!jeffer)jef
(?<=jef)son
(?<!jef)son
/;
print "\n";
foreach $pattern (@pattern_nam) {
print "$pattern : ";
foreach (@nam) {
print "$_ " if (/$pattern/);
}
print "\n";
}
(?=jeffer)jef
(?!jeffer)jef
(?<=jef)son
(?<!jef)son
/;
print "\n";
foreach $pattern (@pattern_nam) {
print "$pattern : ";
foreach (@nam) {
print "$_ " if (/$pattern/);
}
print "\n";
}
產出如下:
(?=\dth)\d : 4th 5th 6th 找出數字(\d)後是th的
(?!\dth)\d : 1st 2nd 3rd 找出數字(\d)後不是th的
(?<=\d)th : 4th 5th 6th 找出th前是數字(th)的 --> 與(?=\dth)結果雖同,但意義不同,括號是找尋符合右邊(或稱behind)的樣式
(?<!\d)th : nth 找出th前不是數字(th)的
(?!\dth)\d : 1st 2nd 3rd 找出數字(\d)後不是th的
(?<=\d)th : 4th 5th 6th 找出th前是數字(th)的 --> 與(?=\dth)結果雖同,但意義不同,括號是找尋符合右邊(或稱behind)的樣式
(?<!\d)th : nth 找出th前不是數字(th)的
(?=jeffer)jef : jeffery jefferson 找出jef開頭而且叫jeffer
(?!jeffer)jef : jef jeff jeffs jefson 找出jef開頭但不叫jeffer的
(?<=jef)son : jefson 找出son前是叫jef的
(?<!jef)son : jefferson 找出son前不是前jef的 --> jeferrson
(?!jeffer)jef : jef jeff jeffs jefson 找出jef開頭但不叫jeffer的
(?<=jef)son : jefson 找出son前是叫jef的
(?<!jef)son : jefferson 找出son前不是前jef的 --> jeferrson
(?=<pattern>)與(?!<pattern>)為look ahead
(?<=<pattern>)與(?<!<pattern>)為look behind
曾經做過EDI的系統,EDI係以加號、冒號、單引號作為分隔字元,而問號則是跳脫字元,若要進行split,分隔字元就排除前面是問號的情況,以逗號為分隔字元的話,那pattern應該是(?<!\?),,因為問號本身也是Regex裡特殊字元,前面應當加斜線。
全站熱搜