理解一个简单的Perl构造
作为一名非Perl程序员,我想确定我已经很好地理解了一个将要移植到Python的结构 使用时:理解一个简单的Perl构造,perl,Perl,作为一名非Perl程序员,我想确定我已经很好地理解了一个将要移植到Python的结构 使用时: if (s/^([$PChar])(.)/$2/) { print $1,"\n"; $finished = 0; } $1、$2等与正则表达式匹配 s/搜索/替换为/ 我真正不确定的是,匹配/替换是否在打印$1之前完成?它是在当前缓冲区(即$F,即$uu逐行读取,在其空格字符上拆分)内“就地”完成的吗?它正在改变它(因此,如果我理解清楚的话,([$PChar]),当字符串的@开头在上述
if (s/^([$PChar])(.)/$2/) {
print $1,"\n";
$finished = 0;
}
- $1、$2等与正则表达式匹配
- s/搜索/替换为/
@F = split;
for( $j=0; $j<=$#F; $j++) {
my $suffix="";
$_ = $F[$j];
# separate punctuation and parentheses from words
do {
$finished = 1;
# cut off preceding punctuation
if (s/^([$PChar])(.)/$2/) {
print $1,"\n";
$finished = 0;
}
# cut off trailing punctuation
if (s/(.)([$FChar])$/$1/) {
$suffix = "$2\n$suffix";
$finished = 0;
}
@F=split;
对于($j=0;$jIf)如果您要发布示例代码,请不要发布tar.gz文件,将纯文本复制并粘贴到一些在线代码板上,如codepad.org。@TLP,好的,谢谢!我直到现在才知道codepart.org:)问题中的注释表明$PChar包含一个标点符号字符串。我没有看tar,但是让我们假设$PChar='\\,:;]'代码>。然后([$PChar])
将恰好捕获一个字符,,,,:
,代码>。一种更简单的方法是,只留下第二个括号,说s/^([$PChar])/
,因为它无论如何都不会触及下一个字符。然后,剩下的进程将继续使用以第二个字符开头的字符串作为新的缓冲区$\ux?还是继续使用所有字符串作为$保持不变?Simbabque:是的,它实际上是标点符号的开头符号,就像开头的括号一样。@user1340802:替换是内联的,这意味着$\uu
的内容被更改(第一个字符被删除)。可能是他正在看的剧本at@TLP)谢谢你。很抱歉,我没有找到这个,因为它是我指向的原始网站的一面镜子。
# try to delete the first character from the string contained in
# $_ if that character is one of the characters contained in
# the string $PChar. The deletion is done by replace the first and
# second character by only the second character.
if (s/^([$PChar])(.)/$2/) {
# if the replacement was successful, print the deleted character.
print $1,"\n";
$finished = 0;
}