Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Perl:如何仅对大写字母字符进行编码和解码_Regex_Perl_Encoding_Character Encoding_Decoding - Fatal编程技术网

Regex Perl:如何仅对大写字母字符进行编码和解码

Regex Perl:如何仅对大写字母字符进行编码和解码,regex,perl,encoding,character-encoding,decoding,Regex,Perl,Encoding,Character Encoding,Decoding,我现在处理这个问题已经有一段时间了。我有拉丁字母表中的字符,希望它们只编码为大写字母字符串。是否有任何模块可以做到这一点?或者任何我可以修改为只使用uc alpha字符的BaseX编码 目前,我已经使用正则表达式替换实现了其中的一部分,但它只包含几个字符,显然效率不高:) 无论如何,如果无法通过模块或函数来处理, 有没有办法通过正则表达式高效地实现这一点 我想到了一个tr/[\+,\-,…]/[加,减,…]/cd 但tr似乎只按字符替换字符,而不是按字符序列替换字符:( 有什么想法吗 achim

我现在处理这个问题已经有一段时间了。我有拉丁字母表中的字符,希望它们只编码为大写字母字符串。是否有任何模块可以做到这一点?或者任何我可以修改为只使用uc alpha字符的BaseX编码

目前,我已经使用正则表达式替换实现了其中的一部分,但它只包含几个字符,显然效率不高:)

无论如何,如果无法通过模块或函数来处理, 有没有办法通过正则表达式高效地实现这一点

我想到了一个
tr/[\+,\-,…]/[加,减,…]/cd

但tr似乎只按字符替换字符,而不是按字符序列替换字符:(

有什么想法吗


achim

您可以使用Base32编码,包含26个大写字母和6位数字:

只需将
$code
数组更改为您想要使用的任何字符集

编辑:哎哟,刚才注意到你是Perl而不是PHP,对不起。你应该能够在CPAN上找到一个Base32模块,它做同样的事情


编辑2:FWIW,我在CPAN上看到Convert::Base32、Encode::Base32和MIME::Base32。

您可以使用Base32编码,包含26个大写字母和6位数字:

只需将
$code
数组更改为您想要使用的任何字符集

编辑:哎哟,刚才注意到你是Perl而不是PHP,对不起。你应该能够在CPAN上找到一个Base32模块,它做同样的事情


编辑2:FWIW,我在CPAN上看到Convert::Base32、Encode::Base32和MIME::Base32。

Base64编码生成十六进制输出,意味着16个可能的字符。因为字母表有26个,所以您可以用数字交换数字。然后,您将只使用字母表中的16个字符,但您将有一个仅由字母组成的字符串这是一个奇怪的问题(看起来像是家庭作业)但是它可以做到这一点。

Base64编码生成十六进制输出,意味着16个可能的字符。因为字母表有26个,所以你可以用数字交换数字。然后,你将只使用字母表中的16个字符,但你将有一个只由字母组成的字符串,在这个字符串中,编码和解码非常容易这是一个奇怪的问题(看起来像是家庭作业),但它会起作用。

你指定了一个非常有损的翻译…这可能不令人满意

但是:

#!/usr/bin/perl 严格使用; 使用警告; 使用5.012; #7095527/perl如何仅对大写字母字符进行编码和解码 my$string=“abcDEFghijklMNO1234567890pqr+)!@}{; my@arr=split/,uc($string); my(@intermediate,$char); 我的$char(@arr){ 如果($char=~/[A-Z]/){ 说“发现了谜字符(可能是uc'ed):$char”; }否则{ 在第17行说“WTF”\$char是!~/[A-Z]/:$char”; 下一个 } } =总产量: >SO7095527.pl 发现谜字符(可能是uc'ed):A 发现谜语字符(可能是uc'ed):B 发现谜字符(可能是uc'ed):C 发现谜字符(可能是uc'ed):D 发现谜字符(可能是uc'ed):E 发现谜字符(可能是uc'ed):F 发现谜字符(可能是uc'ed):G 发现谜团字符(可能是uc'ed):H 发现谜团字符(可能是uc'ed):我 发现谜语字符(可能是uc'ed):J 发现谜字符(可能是uc'ed):K 发现谜团字符(可能是uc'ed):L 发现谜字符(可能是uc'ed):M 发现谜字符(可能是uc'ed):N 发现谜语字符(可能是uc'ed):O 第17行的WTF?$char是!~/[A-Z]/:1 第17行的WTF?$char是!~/[A-Z]/:2 第17行的WTF?$char是!~/[A-Z]/:3 第17行的WTF?$char是!~/[A-Z]/:4 第17行的WTF?$char是!~/[A-Z]/:5 第17行的WTF?$char是!~/[A-Z]/:6 第17行的WTF?$char是!~/[A-Z]/:7 第17行的WTF?$char是!~/[A-Z]/:8 第17行的WTF?$char是!~/[A-Z]/:9 第17行的WTF?$char是!~/[A-Z]/:0 发现谜团字符(可能是uc'ed):P 发现谜团字符(可能是uc'ed):Q 发现谜字符(可能是uc'ed):R WTF?$char在第17行是!~/[A-Z]/:+ WTF?$char在第17行是!~/[A-Z]/:_ WTF?$char在第17行是!~/[A-Z]/:) ... =切割
请注意,如果消息将加油位置指定为“73N 39W”,潜艇船长将得到无用的指示。…

您指定了一个非常有损的翻译…这可能不令人满意

但是:

#!/usr/bin/perl 严格使用; 使用警告; 使用5.012; #7095527/perl如何仅对大写字母字符进行编码和解码 my$string=“abcDEFghijklMNO1234567890pqr+)!@}{; my@arr=split/,uc($string); my(@intermediate,$char); 我的$char(@arr){ 如果($char=~/[A-Z]/){ 说“发现了谜字符(可能是uc'ed):$char”; }否则{ 在第17行说“WTF”\$char是!~/[A-Z]/:$char”; 下一个 } } =总产量: >SO7095527.pl 发现谜字符(可能是uc'ed):A 发现谜语字符(可能是uc'ed):B 发现谜字符(可能是uc'ed):C 发现谜字符(可能是uc'ed):D 发现谜字符(可能是uc'ed):E 发现谜字符(可能是uc'ed):F 发现谜字符(可能是uc'ed):G 发现谜团字符(可能是uc'ed):H 发现谜团字符(可能是uc'ed):我 发现谜语字符(可能是uc'ed):J 发现谜字符(可能是uc'ed):K 发现谜团字符(可能是uc'ed):L 发现谜字符(可能是uc'ed):M 发现谜字符(可能是uc'ed):N 发现谜语字符(可能是uc'ed):O 第17行的WTF?$char是!~/[A-Z]/:1 WTF?$char在第行 #!/usr/bin/perl use strict; use warnings; use 5.012; # 7095527/perl-how-to-encode-and-decode-characters-in-uppercase-alpha-letters-only my $string = "abcDEFghijklMNO1234567890pqr+_)!@#}{?"; my @arr = split //, uc($string); my (@intermediate, $char); for my $char(@arr) { if ($char =~ /[A-Z]/) { say "ENIGMA char found (possibly uc'ed): $char"; } else { say "WTF? \$char at line17 is !~ /[A-Z]/: $char"; next; } } =head OUTPUT: > SO7095527.pl ENIGMA char found (possibly uc'ed): A ENIGMA char found (possibly uc'ed): B ENIGMA char found (possibly uc'ed): C ENIGMA char found (possibly uc'ed): D ENIGMA char found (possibly uc'ed): E ENIGMA char found (possibly uc'ed): F ENIGMA char found (possibly uc'ed): G ENIGMA char found (possibly uc'ed): H ENIGMA char found (possibly uc'ed): I ENIGMA char found (possibly uc'ed): J ENIGMA char found (possibly uc'ed): K ENIGMA char found (possibly uc'ed): L ENIGMA char found (possibly uc'ed): M ENIGMA char found (possibly uc'ed): N ENIGMA char found (possibly uc'ed): O WTF? $char at line17 is !~ /[A-Z]/: 1 WTF? $char at line17 is !~ /[A-Z]/: 2 WTF? $char at line17 is !~ /[A-Z]/: 3 WTF? $char at line17 is !~ /[A-Z]/: 4 WTF? $char at line17 is !~ /[A-Z]/: 5 WTF? $char at line17 is !~ /[A-Z]/: 6 WTF? $char at line17 is !~ /[A-Z]/: 7 WTF? $char at line17 is !~ /[A-Z]/: 8 WTF? $char at line17 is !~ /[A-Z]/: 9 WTF? $char at line17 is !~ /[A-Z]/: 0 ENIGMA char found (possibly uc'ed): P ENIGMA char found (possibly uc'ed): Q ENIGMA char found (possibly uc'ed): R WTF? $char at line17 is !~ /[A-Z]/: + WTF? $char at line17 is !~ /[A-Z]/: _ WTF? $char at line17 is !~ /[A-Z]/: ) ... =cut
%subs = ( '+' => 'PLUS' );
my $pat = join '|', map quotemeta, keys %subs;
s/($pat)/$subs{$1}/g;
sub bytes_to_base16 {
   my $e = unpack('H*', $_);
   $e =~ tr/0123456789ABCDEFabcdef/ABCDEFGHIJKLMNOPKLMNOP/;
   return $e;
}

sub base16_to_bytes {
   my $e = $_[0];
   $e =~ tr/ABCDEFGHIJKLMNOP/0123456789ABCDEF/;
   return pack('H*', $_);
}
$ perl -MMath::BigInt -MMath::BigFloat -E'
   my $n = Math::BigInt->new(1);
   my $bs = 0;
   for (1..10) {
      $n <<= 8;
      ++$bs;
      my $bd16 = 2*$bs;
      my $bd26 = Math::BigFloat->new($n)->blog(26, 5)->bceil->numify;
      say sprintf "%2d bytes takes %2d base16 digits or %2d base26 digits.".
                  " base26 is %3.0f%% of the size of base16.",
         $bs, $bd16, $bd26, $bd26/$bd16*100;
      }
'
 1 bytes takes  2 base16 digits or  2 base26 digits. base26 is 100% of the size of base16.
 2 bytes takes  4 base16 digits or  4 base26 digits. base26 is 100% of the size of base16.
 3 bytes takes  6 base16 digits or  6 base26 digits. base26 is 100% of the size of base16.
 4 bytes takes  8 base16 digits or  7 base26 digits. base26 is  88% of the size of base16.
 5 bytes takes 10 base16 digits or  9 base26 digits. base26 is  90% of the size of base16.
 6 bytes takes 12 base16 digits or 11 base26 digits. base26 is  92% of the size of base16.
 7 bytes takes 14 base16 digits or 12 base26 digits. base26 is  86% of the size of base16.
 8 bytes takes 16 base16 digits or 14 base26 digits. base26 is  88% of the size of base16.
 9 bytes takes 18 base16 digits or 16 base26 digits. base26 is  89% of the size of base16.
10 bytes takes 20 base16 digits or 18 base26 digits. base26 is  90% of the size of base16.
$ perl -MMath::BigInt -MMath::BigFloat -E'
   my $bs = 0;
   for (1..10) {
      ++$bs;
      my $bd16 = 2*$bs;
      my $bd26 = int($bs/4)*7 + ($bs%4)*2;
      say sprintf "%2d bytes takes %2d base16 digits or %2d base26 digits.".
                  " base26 is %3.0f%% of the size of base16.",
         $bs, $bd16, $bd26, $bd26/$bd16*100;
      }
'
 1 bytes takes  2 base16 digits or  2 base26 digits. base26 is 100% of the size of base16.
 2 bytes takes  4 base16 digits or  4 base26 digits. base26 is 100% of the size of base16.
 3 bytes takes  6 base16 digits or  6 base26 digits. base26 is 100% of the size of base16.
 4 bytes takes  8 base16 digits or  7 base26 digits. base26 is  88% of the size of base16.
 5 bytes takes 10 base16 digits or  9 base26 digits. base26 is  90% of the size of base16.
 6 bytes takes 12 base16 digits or 11 base26 digits. base26 is  92% of the size of base16.
 7 bytes takes 14 base16 digits or 13 base26 digits. base26 is  93% of the size of base16.
 8 bytes takes 16 base16 digits or 14 base26 digits. base26 is  88% of the size of base16.
 9 bytes takes 18 base16 digits or 16 base26 digits. base26 is  89% of the size of base16.
10 bytes takes 20 base16 digits or 18 base26 digits. base26 is  90% of the size of base16.
my @syms = ('A'..'Z');
my %syms = map { $syms[$_] => $_ } 0..$#syms;

sub bytes_to_base26 {
   my $e = '';

   my $full_blocks = int(length($_[0]) / 4);
   for (0..$full_blocks-1) {
      my $block = unpack('N', substr($_[0], $_*4, 4));
      $e .= join '', @syms[
         $block / 26**6 % 26,
         $block / 26**5 % 26,
         $block / 26**4 % 26,
         $block / 26**3 % 26,
         $block / 26**2 % 26,
         $block / 26**1 % 26,
         $block / 26**0 % 26,
      ];
   }

   my $extra = substr($_[0], $full_blocks*4);
   for my $block (unpack('C*', $extra)) {
      $e .= join '', @syms[
         $block / 26**1 % 26,
         $block / 26**0 % 26,
      ];
   }

   return $e;
}

sub base26_to_bytes {
   my $d = '';

   my $full_blocks = int(length($_[0]) / 7);
   for (0..$full_blocks-1) {
      my $block = 0;
      $block = $block*26 + $syms{$_} for unpack '(a)*', substr($_[0], $_*7, 7);
      $d .= pack('N', $block);
   }

   my $extra = substr($_[0], $full_blocks*7);
   my @extra = unpack('(a)*', $extra);
   while (@extra) {
      my $block = 0;
      $block = $block*26 + $syms{ shift(@extra) };
      $block = $block*26 + $syms{ shift(@extra) };
      $d .= pack('C', $block);
   }

   return $d;
}
#!/usr/bin/perl
#Tinigma 2010 Usage:tinigma.pl 123 rng ini "GHWVYYDVPQGEWQWVT"
($n,$o,$p)=map(ord()-65,split//,uc$ARGV[1]);($z,$y,$x)=map(ord
()-65,split//,uc$ARGV[2]);($l,$m,$r)=map$_-1,split//,$ARGV[0];
$t=uc$ARGV[3];$t=~s/[^A-Z]//g;$b=26;$j=0;@N=qw(7 25 11 6 1);@R
=('EKMFLGDQVZNTOWYHXUSPAIBRCJ'x3,'AJDKSIRUXBLHWTMCQGZNPYFVOE'x
3,'BDFHJLCPRTXVZNYEIWGAKMUSQO'x3,'ESOVPZJAYQUIRHXLNFTGKDCMWB'x
3,'VZBRGITYUPSDNHLXAWMJQOFECK'x3,'YRUHQSLDPXNGOKMIEBFZCWVJAT'x
3);@t=split//,$t;for$v(@R){$i=0;for(split//,$v){$c=ord($_)-65;
$F[$j][$i]=$c;$R[$j][$c+$b*(int($i/$b))]=$i;$i++}$j++}@S=@{$F[
5]};$f=$y==$F[$m][$N[$m]]?1:0;$i=0;for(@t){if($f){$y++;$y%=$b;
$z++;$z%=$b;$f=0}if($x==$F[$r][$N[$r]]){$y++;$y%=$b;if($y==$F[
$m][$N[$m]]){$f=1}}$x++;$x%=$b;$e.=chr(($R[$r][$R[$m][$R[$l][$
S[$F[$l][$F[$m][$F[$r][ord($_)-39+$x-$n]-$x+$n+$y-$o]-$y+$o+$z
-$p]-$z+$p]+$z-$p]-$z+$p+$y-$o]-$y+$o+$x-$n]-$x+$n)%$b+65)}
print"$e\n"