challenge 文字列からの情報抽出

与えられた文字列から特定の条件を満たす文字列を抽出するコードを書いてください。 状況としてはテキスト形式で渡された原稿の中から、画像のファイル名を抽出するようなものをイメージしてください。

サンプル入力

aaa abc-hidden.png>hoge-big.jpeg
---foo-hidden-small.gif|^_^a.bmp
--hiddena-hoge.png<=not hidden~~
--small.jpg<=not small(^_^)
normal-small-big.hoge

サンプル出力

name:'abc', ext:'png', size: normal hidden: True
name:'hoge', ext:'jpeg', size: big hidden: False
name:'foo', ext:'gif', size: small hidden: True
name:'a', ext:'bmp', size: normal hidden: False
name:'hoge', ext:'png', size: normal hidden: False
name:'small', ext:'jpg', size: normal hidden: False
name:'small', ext:'hoge', size: big hidden: False

探すべき文字列は下の条件を満たします

  • アルファベットと1個のピリオド、ハイフンで構成される
  • 前後にはアルファベットではない文字がある(abcd.jpgがaaaabcd.jpghogeなどと書かれていることはない)
  • ピリオドの後ろは拡張子で、アルファベットだけで構成されている
  • ピリオドの直前に-hidden, -small, -bigがある場合には特殊な意味がある。複数個ある場合(a-hidden-big.jpgなど)も同じ
  • ファイル名に-hiddenと-smallまたは-hiddenと-bigの両方が含まれる場合は-hiddenの方が先にある
  • 特殊な意味の-hidden, -small, -big以外でハイフンが使われることはない
  • 特殊な意味の-smallと-bigの両方が付くことはない

出力は以下の条件を満たす必要があります

  • ファイル名が出現した順に表示される
  • ファイル名に-hiddenが含まれるかどうかを真偽値で表示する
  • ファイル名に-smallまたは-bigが含まれる場合はsmallまたはbigと、含まれない場合はnormalと表示する
  • -hidden, -small, -bigを取り除いたファイル名部分と、拡張子を表示する

このお題は、正規表現のグループに名前をつけて連想配列として取得できるPythonからの挑戦状です。

Posted feedbacks - 秀丸マクロ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    $output = "";
    disabledraw;
    gofiletop;
    while( true ) {
        searchdown2 "[A-Za-z]+(-hidden)?(-(small|big))?\\.[A-Za-z]+", regular;
        if ( !result ) {
            break;
        }
        #x = x;
        #y = y;
        searchdown "[-.]", regular;
        $name = gettext( #x, #y, x, y );
        $size = "normal";
        $hidden = "False";
        while( code == '-' ) {
            movetolineno column + 1 + strlen( "-" ), lineno;
            #x = x;
            #y = y;
            searchdown "[-.]", regular;
            $option = gettext( #x, #y, x, y );
            if ( $option == "big" ) {
                $size = "big";
            }
            if ( $option == "small" ) {
                $size = "small";
            }
            if ( $option == "hidden" ) {
                $hidden = "True";
            }
        }
        movetolineno column + 1 + strlen( "." ), lineno;
        #x = x;
        #y = y;
        searchdown "[^A-Za-z]", regular;
        if ( !result ) {
            gofileend;
        }
        $ext = gettext( #x, #y, x, y );
        $output = $output + "name:'" + $name + "', ext:'" + $ext+"', size: " + $size+" hidden: " + $hidden + "\n";
    }
    enabledraw;
    newfile;
    insert $output;

Index

Feed

Other

Link

Pathtraq

loading...