正規表現実用講座2


例3.下記の英文はイソップ物語のなかの"狐とカラス"の話です。

この物語で使われている単語をABC順に重複することなく並べてみましょう。

インデックスを作成するのに便利です。

There once was a big black crow sitting high up in the trees. In his beak he had a nice, round cheese. 

Along came a fox, as clever as they come, "Mmmm," he thought. "I'd like to have a bite of that cheese. It will be easy to get some...." 

"Oh crow," called fox, "if your voice is half as beautiful as those fine feathers I see, it would please my ears to hear you sing a little melody!" 

Well, crow had never heard anyone say such a complimentary thing. So, he opened up his beak and he began to squawk and sing. 

Down fell the cheese into the waiting mouth of the fox below. 

"Oh no!" squawked the crow, "you've stolen my dinner!" 

"Not at all!" said the fox, licking his lips. "It was a fair enough trade! Vain crow, with your head up in the trees! You got the compliments, and I got the cheese!" 

英単語のリストを作成するために次の工程が必要になります。

1.大文字を小文字にする。

2."スペース"、","、"."、"""、"!"を除く。

3.残った英単語をabc順に並べ替える。

4.重複する語を除く。

最後にこれらの工程をバッチファイルでひとつにまとめると数秒で全処理が終了します。



それでは始めましょう。

工程1.大文字を小文字にする。

プログラム1

y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

[結果]

there once was a big black crow sitting high up in the trees. in his beak he had a nice, round cheese. 

along came a fox, as clever as they come, "mmmm," he thought. "i'd like to have a bite of that cheese. it will be easy to get some...." 

"oh crow," called fox, "if your voice is half as beautiful as those fine feathers i see, it would please my ears to hear you sing a little melody!" 

well, crow had never heard anyone say such a complimentary thing. so, he opened up his beak and he began to squawk and sing. 

down fell the cheese into the waiting mouth of the fox below. 

"oh no!" squawked the crow, "you've stolen my dinner!" 

"not at all!" said the fox, licking his lips. "it was a fair enough trade! vain crow, with your head up in the trees! you got the compliments, and i got the cheese!" 

工程2."スペース"、","、"."、"""、"!"を除く。

プログラム2

s/[ ,."!]/\n/g


[結果]

there

once

was

a

big

black

crow

sitting

high

up

in

the

trees



in

his

beak

he

had

a

nice



round

cheese





along

came

a

fox



as

clever

as

they

come





mmmm





he

thought





i'd

like

to

have

a

bite

of

that

cheese



it

will

be

easy

to

get

some















oh

crow





called

fox





if

your

voice

is

half

as

beautiful

as

those

fine

feathers

i

see



it

would

please

my

ears

to

hear

you

sing

a

little

melody







well



crow

had

never

heard

anyone

say

such

a

complimentary

thing



so



he

opened

up

his

beak

and

he

began

to

squawk

and

sing





down

fell

the

cheese

into

the

waiting

mouth

of

the

fox

below







oh

no





squawked

the

crow





you've

stolen

my

dinner









not

at

all





said

the

fox



licking

his

lips





it

was

a

fair

enough

trade



vain

crow



with

your

head

up

in

the

trees



you

got

the

compliments



and

i

got

the

cheese


工程3.残った英単語をabc順に並べ替える。

プログラム3

既存のツール


[結果]

a

a

a

a

a

a

a

all

along

and

and

and

anyone

as

as

as

as

at

be

beak

beak

beautiful

began

below

big

bite

black

called

came

cheese

cheese

cheese

cheese

clever

come

complimentary

compliments

crow

crow

crow

crow

crow

dinner

down

ears

easy

enough

fair

feathers

fell

fine

fox

fox

fox

fox

get

got

got

had

had

half

have

he

he

he

he

head

hear

heard

high

his

his

his

i

i

i'd

if

in

in

in

into

is

it

it

it

licking

like

lips

little

melody

mmmm

mouth

my

my

never

nice

no

not

of

of

oh

oh

once

opened

please

round

said

say

see

sing

sing

sitting

so

some

squawk

squawked

stolen

such

that

the

the

the

the

the

the

the

the

the

there

they

thing

those

thought

to

to

to

to

trade

trees

trees

up

up

up

vain

voice

waiting

was

was

well

will

with

would

you

you

you've

your

your


4.重複する語を除く。

プログラム4

#同一行の繰り返しを1行に

:loop

$q

N

s/^\(.*\)\n\1$/\1/

t loop

P

s/^.*\n//

b loop


[結果]

a

all

along

and

anyone

as

at

be

beak

beautiful

began

below

big

bite

black

called

came

cheese

clever

come

complimentary

compliments

crow

dinner

down

ears

easy

enough

fair

feathers

fell

fine

fox

get

got

had

half

have

he

head

hear

heard

high

his

i

i'd

if

in

into

is

it

licking

like

lips

little

melody

mmmm

mouth

my

never

nice

no

not

of

oh

once

opened

please

round

said

say

see

sing

sitting

so

some

squawk

squawked

stolen

such

that

the

there

they

thing

those

thought

to

trade

trees

up

vain

voice

waiting

was

well

will

with

would

you

you've

your


これで単語リストが出来上がりました。