Regular expressions 正規表現
平成17年3月30日水曜の日記
天気: 晴 行事: 赤来中
天気: 晴 行事: 赤来中
今日もぜんぜん仕事がありませんでした。ほとんどの日、やっぱり、ホームページに作っていました。今日のことはザ・トレブ・レポートをきれいになりました。一番難しい仕事はテクストファイルからXHTMLページまで変わるPerlスクリプトを書きました。Perl正規表現はすごく読みにくいけれどとっても強く便利します。日本語も少し勉強しました。今晩、飯 南町教育委員会送別会でありました。頓原地域ALTのアリシアさんも来ました。アリシ アさんは僕より日本語が上手になりました。うらやましいです。
おまけ:このPerl正規表現は何をしますか?
暗示:この表現の前、xは限界がタブに定められたファイル・リストです。yはxの中で ファイル名。一つのことでこの表現が失敗します。そのことは何でしょうか?
おまけ:このPerl正規表現は何をしますか?
$x=~s!^.*[^\(\w|.\)]+([\(\w|.\)]+)(?=[^\(\w|.\)]*$y).*$!$1!sx;
暗示:この表現の前、xは限界がタブに定められたファイル・リストです。yはxの中で
Journal for 2005-3-30 Wed.
Weather: Sunny Plan: Akagi JHS
Weather: Sunny Plan: Akagi JHS
I didn't have any work to do today. As you might have guess, I spent most of the day working on my web site. The task for today was to make The Trev Reports look SOOO GOOD. (alright, chill) The toughest part was writing a Perl script to convert a text file into a XHTML page. The regular expressions in Perl are really hard to read, but are very powerful and useful. I also studied a little Japanese. Tonight was the Iinan Town Board of Education Going Away Party for staff that are changing positions. Alicia, the ALT in Tonbara, was also there. Alicia's Japanese has become better than mine. I am jealous.
Bonus: What does this Perl regular expression do?
Hint: before this expression, x is a tab-delimited file listing. y is the name of a file in x. There is one situation where this expression fails. Do you know what it is?
Bonus: What does this Perl regular expression do?
$x=~s!^.*[^\(\w|.\)]+([\(\w|.\)]+)(?=[^\(\w|.\)]*$y).*$!$1!sx;
Hint: before this expression, x is a tab-delimited file listing. y is the name of a file in x. There is one situation where this expression fails. Do you know what it is?
Chinese Reading (音読み): | |
Reading | Romaji |
アン | an |
Japanese Reading (訓読み): | |
Reading | Romaji |
くらい | kurai |
Vocabulary (単語): | |||
Word | Reading | Romaji | Translation |
暗い | くらい | kurai | dark |
暗記 | あんき | anki | memorization |
暗殺 | あんさつ | ansatsu | assassination |
暗算 | あんざん | anzan | mental arithmetic |
暗示 | あんじ | anji | hint |
暗号 | あんごう | angou | password |
明暗 | めいあん | meian | light and darkness |
regular expressions
Re: regular expressions
Re: regular expressions
Re: regular expressions
That is harsh. I bet SSH won't work either. Most likely they are blocking everything but web, smtp, and pop ports. That sucks. Nothing is blocked here. I am access SDF from SSH using PuTTY.
Re: regular expressions
Re: regular expressions
regex help
$url is http://www.archive.org/download/Chinkapin_Hunters/CH1_Flippen_Jenny.mp3
// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i", $url, $matches);
$host = $matches[2];
// get last two segments of host name
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
$domain = $matches[0];
$domain is archive.org
but how can I get "CH1_Flippen_Jenny.mp3" ? I'll have to split url by "/" i imagine... then I also want to get ".mp3"
Re: regex help
The key is to just grab (using ()) the very end using $, and then just have a delimiterright before the thing you grab.
I'm sorry I don't know PHP, so I can't churn out that code, but I think you might be able to get it from this. (one of these days I'll have to learn PHP...)
#!/usr/pkg/bin/perl -w
my $url="http://www.archive.org/download/Chinkapin_Hunters/CH1_Flippen_Jenny.mp3";
my $file=$url;
my $ext=$url;
$file=~s/^.+\/(.+$)/$1/i;
$ext=~s/^.+(\..+$)/$1/i;
print "$file\n";
print "$ext\n";
Re: regex help
?
Re: regex help
$1 is a variable assigned to the value in the first pararenthisized regex in the search regex.
When you do:
$file =~ s/^.+\/(.+$)/$1/i;
What you wil get in $1 is what is inside (), mainly .+ (one or more of any character) $ (at the end of the line) that is after the last '/'. If you read the regex backward it might make more sense.
Re: regex help
Re: regex help
preg_match("/^.+\/(.+$)/i", $url, $matches);
$filename = $matches[2];
preg_match("/^.+(\..+$)/i", $url, $matches);
$extension = $matches[2];
Re: regex help
The key here is understanding what in in the matches array. matches[1] is the first parenthisized value in the regex, and matches[2] is the second parenthisized value (embedded in the first).
Answer:
$filename</b> and the extension is $extension.";
?>
the monster you've created
It works! I'm always amazed when that happens. This doesn't mean I'll stop bugging you for help though ;~)
Re: the monster you've created