dポイントプレゼントキャンペーン実施中!

言語:perl5.00404

機種依存文字である、ローマ数字を変換したいのですが、
例:ローマ数字の1~10を、I,II,III,IV,V,・・・と変換したい。 

試みた方法としては、
&jcode::tr(\$str, "\xAD\xB5", "I");
&jcode::tr(\$str, "\xAD\xB6", "II");
&jcode::tr(\$str, "\xAD\xB7", "III");
&jcode::tr(\$str, "\xAD\xB8", "IV");
&jcode::tr(\$str, "\xAD\xB9", "V");

jcodeを使用して変換。
この方法だと、ローマ数字の1~3は、全て"I"としか
変換してくれず困っています。
(1文字目しか変換されないようなのです。)


これではいけないと考え、正規表現で以下のように試みたのですが、

$eucpre = qr{(?<!\x8F)};
$eucpost = qr{
(?=
(?:[\xA1-\xFE][\xA1-\xFE])* # JIS X 0208 が 0文字以上続いて
(?:[\x00-\x7F\x8E\x8F]|\z) # ASCII, SS2, SS3 または終端
)
}x;

$str =~ s/$eucpre(?:\xAD\xB5)$eucpost/$1I/g;
$str =~ s/$eucpre\Q\xAD\xB5\E$eucpost/$1I/g;

$str =~ s/$eucpre(?:\xAD\xB6)$eucpost/$1II/g;
$str =~ s/$eucpre\Q\xAD\xB6\E$eucpost/$1II/g;

$str =~ s/$eucpre(?:\xAD\xB7)$eucpost/$1III/g;
$str =~ s/$eucpre\Q\xAD\xB7\E$eucpost/$1III/g;

$str =~ s/$eucpre(?:\xAD\xB8)$eucpost/$1IV/g;
$str =~ s/$eucpre\Q\xAD\xB8\E$eucpost/$1IV/g;

$str =~ s/$eucpre(?:\xAD\xB9)$eucpost/$1V/g;
$str =~ s/$eucpre\Q\xAD\xB9\E$eucpost/$1V/g;

これだとperlのバージョンが対応していない(perl5.005以上だとできる)のでこの策もだめで、困り果てています。どなたかよい方法を教えてください。

A 回答 (2件)

その正規表現による方法は、参考URLの「Perlメモ:正しくパターンマッチさせる」で紹介されているものですが、そこにはPerl5.005より前の環境でも利用可能な方法も載っていますので、そちらを参考にされるとよいでしょう。



参考URL:http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

試みたのですが、不可解な現象にぶちあたりました。

【正常に動作】
$ascii = "[\x00-\x7F]";
$twoBytes = "[\x8E\xA1-\xFE][\xA1-\xFE]";
$threeBytes = "\x8F[\xA1-\xFE][\xA1-\xFE]";

$pattern = "(2)";
$replace = "II";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(3)";
$replace = "III";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(4)";
$replace = "IV";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(5)";
$replace = "V";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

【Internal Errorとなってしまう】
$ascii = "[\x00-\x7F]";
$twoBytes = "[\x8E\xA1-\xFE][\xA1-\xFE]";
$threeBytes = "\x8F[\xA1-\xFE][\xA1-\xFE]";

$pattern = "(1)";
$replace = "I";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(2)";
$replace = "II";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(3)";
$replace = "III";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(4)";
$replace = "IV";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;

$pattern = "(5)";
$replace = "V";
$str =~ s/\G((?:$ascii|$twoBytes|$threeBytes)*?)(?:$pattern)/$1$replace/g;


ローマ数字の(1)を変換しようとするとエラーになってしまうのですが、なぜでしょう?

お礼日時:2004/12/22 21:50

EUCコードですね。


単純に

$str =~ s/\xAD\xB5/I/g;

の様な羅列ではだめなのですか?
見当違いだったらすみません。

この回答への補足

ご回答ありがとうございます。

ご指摘のような変換方法ですと、
例えば、「記記」(B5ADB5AD)といった文字を変換しようとすると、
正しく変換できないという現象に見舞われます。

補足日時:2004/12/21 20:03
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!