Advertisement
cd62131

fullwidth digit <-> ascii digit

Dec 2nd, 2018
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.97 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use open qw(:std :encoding(utf8));
  6. sub num_zen($);
  7. sub zen_num($);
  8. sub num_zen($) {
  9.   my $s = shift;
  10.   my $zen = [ split '', q(0123456789) ];
  11.   $s =~ s/(?<![0-9])(?![0-9]{2}(?:\D|\b))[0-9]+/
  12.       join '', map { $$zen[$_] } split '', $&
  13.     /egx;
  14.   return $s;
  15. }
  16. sub zen_num($) {
  17.   my $s = shift;
  18.   my $zen = [ split //, q(0123456789) ];
  19.   my $tab = { map { $$zen[$_], $_ } 0 .. 9 };
  20.   $s =~ s/(?<![0−9])[-]+/
  21.       join '', map { $$tab{$_} } split '', $&
  22.     /egx;
  23.   return $s;
  24. }
  25. my $p = tell DATA;
  26. while (<DATA>) {
  27.   print num_zen $_;
  28. }
  29. seek DATA, $p, 0;
  30. while (<DATA>) {
  31.   print zen_num $_;
  32. }
  33. seek DATA, $p, 0;
  34. while (<DATA>) {
  35.   print num_zen zen_num $_;
  36. }
  37. __DATA__
  38. 1 11 111 1111 1 11 111 1111
  39. a0a a00a a000a あ0あ あ00あ あ000あ
  40. 00 00 00 00
  41. 000 000 00 000 000 000 000 000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement