perl 中匹配十进制数 ^-?/d+$

11、指定选项
   字符"|"指定两个或多个选择来匹配模式。如:/def|ghi/匹配def或ghi。
   例:检验数字表示合法性
   if ($number =~ /^-?/d+$|^-?0[xX][/da-fa-F]+$/) {
     print ("$number is a legal integer./n");
   } else {
     print ("$number is not a legal integer./n");
   }
   其中 ^-?/d+$ 匹配十进制数字,^-?0[xX][/da-fa-F]+$ 匹配十六进制数字。

 

 

肯定的和否定的预见匹配
   肯定的预见匹配语法为/pattern(?=string)/,其意义为匹配后面为string的模式,相反的,(?!string)意义为匹配后面非string的模式,如:
     $string = "25abc8";
     $string =~ /abc(?=[0-9])/;
     $matched = $&; # $&为已匹配的模式,此处为abc,而不是abc8
模式注释
   PERL5中可以在模式中用?#来加注释,如:
     if ($string =~ /(?i)[a-z]{2,3}(?# match two or three alphabetic characters)/ {
       ...
     }

将下面的代码转为python #!/user/bin/perl use v5.38; use POSIX; die "Usage: perl $0 pattern_file_name\n" unless @ARGV; my $file = $ARGV[0]; my $ilnway = 1; open my $fh, "<", $file or die; my $ofile; if ($file =~ /([^.]+)\.(.*)/) { $ofile = $1."_16way.".$2; } else { $ofile = $file."_16way"; } if (-e $ofile) { die "file already exists"; } open my $ofh, ">", $ofile or die; say "$file ===> $ofile"; my $line; my $pure_line; sub read_line { if ($line = <$fh>) { $pure_line = $line; $pure_line =~ s/;.*//; return 1; } $pure_line = ""; return 0; } my $way = 1; my $last_way_line = ""; sub skip_line { while (read_line()) { if ($pure_line =~ /^\s*$/) { print $ofh $line; } elsif ($pure_line =~ /^\s*;/) { print $ofh $line; } elsif ($pure_line =~ /^\s*MPAT\s+\w+<(\d+)WAY>/) { $ilnway = $1 + 0; my $new_line = $line; $new_line =~ s/<\d+WAY>/<16WAY>/; $new_line =~ s/MPAT\s+\K(\w+)/$1_16WAY/; print $ofh $new_line; while (read_line()) { print $ofh $line; if ($pure_line =~ /^\s*START\b/) { last; } } } elsif ($pure_line =~ /^\s*MODULE\b/) { if ($pure_line =~ /END/) { if ($way >1 and $way <16) { for ($way + 1 .. 16) { print $ofh $last_way_line; } } $way = 1; } print $ofh $line; while (read_line()) { print $ofh $line; if ($pure_line =~ /^\s*START\b/) { last; } } } elsif ($pure_line =~ /^\s*(START|END|STPS|STOP|STPF)\b/) { if ($pure_line =~ /END|STPS|STOP|STPF/) { if ($way >1 and $way < 16) { for ($way +1 .. 16) { print $ofh $last_way_line; } } $way = 1; } print $ofh $line; } else { return 1; } } return 0; } my $rule = 0; my $opcode; my $operand; my $last_opcode; my $last_operand; my $pc = 0; my $idxi_sub = 0; sub select_rule { if ($opcode =~ /^(JSR|RTN|JMP|JNI|JNC)/) { if ($last_opcode eq "NOP") { for ($way + 1 .. 16) { print $ofh $last_way_line; } } $way = 1; return 1; } if ($opcode eq "NOP" and $last_opcode eq "NOP") { return 2; } elsif ($opcode =~ /^(IDXI\d)/) { if ($last_opcode eq "NOP") { for ($way +1 .. 16) { print $ofh $last_way_line; $idxi_sub++; } } if ($pure_line =~ /\bIDXI\d+\s+/) { if ($line =~ /^(.*IDXI\d+\s+#)([0-9a-fA-F]+)(.*)/) { my($l, $m, $t) = ($1,$2,$3); my $new_num = ceil(((hex($m) +2) * $ilnway - $idxi_sub) / 16.0) -2; $idxi_sub = 0; if ($new_num < 0) { my $pre = $l.$m; $pre =~ s/IDXI\d+\s+#[0-9a-fA-F]+/NOP\t/; $line = $pre.$t."\n"; } else { my $hex_string = sprintf("%X", $new_num); $line = $l.$hex_string.$t."\n"; } } elsif ($line =~ /^(.*IDXI\d+\s+)(\d+)(.*)/) { my($l,$m,$t) = ($1,$2,$3); my $new_num = ceil(((hex($m) +2) * $ilnway- $idxi_sub) / 16.0) -2; $idxi_sub = 0; if ($new_num < 0) { my $pre = $l.$m; $pre =~ s/IDXI\d+\s+#[0-9a-fA-F]+/NOP\t/; $line = $pre.$t."\n"; } else { $line =$l.$new_num.$t."\n"; } } } $way = 1; return 3; } elsif ($opcode eq "NOP") { return 0; } else { return 999; } } while (skip_line()) { my $line_num = $.; if ($way ==16) { $way = 1; } if ($pure_line =~ /^\s*(OUT|NOP)/) { if ($pure_line !~ /:/) { if ($way >1 and $way< 16) { for ($way +1 .. 16) { print $ofh $last_way_line; } } $last_opcode = ""; $way = 1; $opcode = "OUT"; $rule = 0; print $ofh $line; next; } } if ($pure_line =~ /^\s*(\w+):\s+(\w+)/) { if ($way >1 and $way< 16) { for ($way +1 .. 16) { print $ofh $last_way_line; } } $last_opcode = ""; $way = 1; $opcode = $2; if ($last_way_line eq "") { if ($line =~ /(.*?):(.*)/) { my $pre = $1; my $remain = $2; $last_way_line = $pre; $last_way_line =~ s/[^\t]/ /g; $last_way_line .= ":$remain\n"; $last_way_line =~ s/;[^\n\r]*/;/; } } $rule = select_rule(); print $ofh $line; } elsif ($pure_line =~ /^(\s+(\w+).*):(.*)/) { if ($line =~ /^(\s+(\w+).*?):(.*)/) { if ($way >1) { $last_opcode = $opcode; } else { $last_opcode = ""; } my $pre = $1; $opcode = $2; my $remain =$3; if ($last_way_line eq "") { $last_way_line = $pre; $last_way_line =~ s/[^\t]/ /g; $last_way_line .= ":$remain\n"; $last_way_line =~ s/;[^\n\r]*/;/; } $rule = select_rule(); if ($rule == 2) { my $new_line = "\t" x 5; print $ofh "$new_line:$remain\n"; $way++; $rule = 0; } else { $way = 1; print $ofh $line; } } } elsif ($pure_line =~ /^\s*:/) { print $ofh $line; $way++; if ($way == $ilnway) { if ($rule == 1 or $rule == 3) { for ($way+ 1 .. 16) { print $ofh $last_way_line; } $way = 1; $last_opcode = $opcode; } elsif ($rule == 999) { for ($way+ 1 .. 16) { print $ofh $last_way_line; } $way = 1; $last_opcode = $opcode; } } if ($pure_line =~ /^\s+:\s+D[_E]/) { $last_way_line = $line; $last_way_line =~ s/;[^\n\r]*/;/; } } else { print $ofh $line; } } close $fh; close $ofh;
08-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值