Log in

No account? Create an account


Regular expressions 正規表現

Regular expressions 正規表現

Previous Entry Share Next Entry
天気: 晴 行事: 赤来中



Journal for 2005-3-30 Wed.
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?

Today's Kanji
Meaning (意味): dark

Chinese Reading (音読み):
Reading Romaji
Japanese Reading (訓読み):
Reading Romaji
Vocabulary (単語):
Word Reading Romaji Translation
暗算あんざんanzanmental arithmetic
明暗めいあんmeianlight and darkness

Trevor Lalish-Menagh
  • I've got the feeling it replaces everything in the line containing the filename $y with the thing listed in the column before the file... whatever that is in the file listing.
    • You are really close. Think about what the /s does to x. If it helps, you can also think of a filename as any combination of alphanumerics, underscores, and/or periods.
  • regular expressions

    I set up an SDF account today... I guess that means I have to learn this stuff now...
    • Re: regular expressions

      Sweet! I'm jumex on there. I often check out the bboard, and right now I7m on com in bcast, if you want to say hi. :)
      • Re: regular expressions

        I'm still "prevalidated" ... also the ports are not open here at the BOE,,, I can use the java applet telnet client, but it's too jumpy to be worth it. Any way to get around?
        • Re: regular expressions

          Have you tried using SSH and port 22?
          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

            Yeah, I usually use putty. Ah well. I bet I could tunnel through my PC at home... but this is my last office day, thankfully.
    • regex help

      I'm working in PHP, but I was thinking you could 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

        This perl code does the trick.
        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;


        print "$file\n";

        print "$ext\n";
        • Re: regex help

          What if there are more than one or no subdirectories at $1/

          • Re: regex help

            It will work regardless of directory sublevels since the delimiter is a '/' and there are at least two '/' characters in 'http://' always.

            $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

        I can finally see what everything is doing, but it isn't giving me what i want... rrr... no errors tho...

        preg_match("/^.+\/(.+$)/i", $url, $matches);
        $filename = $matches[2];
        preg_match("/^.+(\..+$)/i", $url, $matches);
        $extension = $matches[2];
        • Re: regex help

          I strongly recommend that you study Perl regex if you are going to mess around with pcre parsers. The perlre manpage is a good place to start, but there are a lot of primers on the net to help you out. Once you totally understand it, you will never think the same way again. :)

          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).


          $filename</b> and the extension is $extension.";
Powered by LiveJournal.com