Quantcast

Maximum PC

It is currently Sun Apr 20, 2014 7:28 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 17 posts ] 
Author Message
 Post subject: Regular expression fun!!!
PostPosted: Mon Apr 04, 2005 8:42 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Many of us have used them - and those who haven't will truly appreciate them in the future - so using whatever language you like that supports RE's give this simple exercise a shot. =)

Houses are either: new, old or neither (ie null value)
Houses can only come in the following colors: red, blue, and green

#1 Just matching - return either a true or false. Write a RE that matches all of the possible colors of houses and color types:

My house is red.
My house is green.
My house is blue.
My house is black. => returns false

#2 Now do add a replace to #1. No one wants an old looking house, so change any occurance of old to new, like so...
My old house is red. => My new house is red.
My house is blue. => My house is blue.

#3 The previous, add the color orange, and use several colors per sentence.

Match combinations like...
My house has blue walls and red trim.
My house has green walls and blue trim.
etc.

In any order...
My house has red trim and orange walls.
etc.

Still replace... everywhere!
My house has an old red fence. => My house has a new red fence.

(if you're having trouble at this point, it may help to break it down into multiple simpler RE's that solve part of the problem and then think about forming them into more complex RE's).

#3 Match for any number of colors/fixtures (using simple english grammar)....
My house has blue walls, red trim and a red fence.
My house has green walls, blue trim, a red fence, and an orange fence.
etc.

My house has an old red tree house and blue fence. => My house has a new red tree house and blue fence.

#1 and #2 are simple... and good practice. I'll post a solution in Java either T or W depending on my free time. Maybe we'll parse a log file or something more useful next time. ;)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 11:39 am 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
I just found out about them about a week ago. They seem pretty obscure still since when I asked 2 friends majoring in CS they had no idea.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 1:43 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
I have absolutely no idea.. Maybe I'll look them up and try it out.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 3:05 pm 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
I'm told lycos uses them: http://en.wikipedia.org/wiki/Regular_expression


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 6:42 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Looks like a good test for my syntax tree and parser. I'll try it out when I get a chance.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 11:58 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
urmumsacow wrote:
I just found out about them about a week ago. They seem pretty obscure still since when I asked 2 friends majoring in CS they had no idea.

Oh, you'll be amazed how handy they are... =)


Top
  Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 12:02 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Kybo_Ren wrote:
Looks like a good test for my syntax tree and parser. I'll try it out when I get a chance.

Did you get a chance? I've done the first two - I'll leave the third for now in case someone else still wants to try...

Code:
import java.util.*;
public class Practice {
   
    public static void main(String[] args) {
       
        //part 1
        String re = "My house is (red|green|blue)\\.";  // the \\. matches a period, not 'match anything'
        doMatch("My house is red.",re);
        doMatch("My house is green.",re);
        doMatch("My house is blue.",re);
        doMatch("My house is black.",re);
       
        //part 2
        doReplace("My old house is red.");
        doReplace("My house is blue.");


       
        System.exit(0);
    }   
   
    public static boolean doMatch(String s, String re) {
       
        System.out.print("Sentence: " + s);
        if (s.matches(re)) {
            System.out.println("\t matches => return true");
            return true;
        }

        System.out.println("\t does not match => return false");       
        return false;
    }
   
    public static void doReplace(String s) {
        System.out.println("before: " + s);
        s = s.replaceAll(" old ", " new ");  //why are the spaces important?
        System.out.println("after:  " + s);
    }       
}


And the output (I ran it in NetBeans):

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\zd253c\ICD Checker Tools\build\classes
compile-single:
run-single:
Sentence: My house is red. matches => return true
Sentence: My house is green. matches => return true
Sentence: My house is blue. matches => return true
Sentence: My house is black. does not match => return false
before: My old house is red.
after: My new house is red.
before: My house is blue.
after: My house is blue.
BUILD SUCCESSFUL (total time: 2 seconds)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 5:18 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Nah, the only chances I've had I was working on a gametype extractor for Halo 2 so I can edit my gametypes.

I'll try to do it tomorrow.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 5:05 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Sorry, couldn't access my computer and get the source I have for my parser today, and I don't feel like rewriting 1200 lines right now, so maybe tomorrow.

EDIT: I finally got the files.


Last edited by Kybo_Ren on Tue May 03, 2005 7:03 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 7:05 pm 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
Damnit just when I started the tutorial I had to get off.

The RSS importer for azureus supports them so I actually have a (weak) reason to learn them.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 10:18 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
urmumsacow wrote:
Damnit just when I started the tutorial I had to get off.

The RSS importer for azureus supports them so I actually have a (weak) reason to learn them.

Give us examples of what you would want to match (and not match).

Grep'ing for files across the internet... nice. =)


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 7:12 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Wed Jun 16, 2004 11:30 am
Posts: 368
Location: San Antonio, TX
I love RegExs and I hate RegExs. :)


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 8:20 am 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
Well I want to setup my RSSFeed plugin for Azureus to grab the new torrents of <> show... unfortunately I can't just put the name in there because there are releases in several languages and I only want the english one. Luckily the torrent files themselves are named with the following nomenclature and as long as it persists a regex should work. The naming style goes as follows:

n - prefixes all the torrents for this series, assumed to be part of the torrent
133 - a 3 digit number for the episodes number, guess which episode number the example is for ;)
.torrent - obviously at the end of all torrents.

A full blown example of a desireable torrent: n124.torrent

now the releases in the languages I DONT want would look the same, only with an added 2 letter suffix to the ep number, as exampled:

n124br.torrent - the br is for brazillian subtitles. I do NOT want this as I don't read brazillian.

Heres the regex I cooked up to do this for me:
Code:
n[1-2][1-9][1-9]\.torrent
... which works at least in editpad where I tested it.

Unfortunately the RSS feed only uses the title of the shows release and its own sites php index # of the torrent (which is likely to be something like 523373) until you download the file where it sends you "n124.torrent".

I've kinda lost interest in the show so I haven't bothered to try to find a work around or a regex the will properly filter by title name (though that might be hard as I'm not sure the RSSFeed plugin allows you to use regex's for title searching in addition to the fact that the titles of the releases vary in nomenclature).


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 8:21 am 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
...so, do I pass regex's 101? :)


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 23, 2005 10:23 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
I'm not sure. Based on your description, I would have written the following regex...

Code:
n[0-9]{3}\.torrent


The difference being that mine will match something like n333.torrent - yours won't. They may have been what you meant though.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 23, 2005 11:05 am 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
They are only on episode 135, it would take a year to get past the 200 mark. If it matches 300+ it might pull down manga releases.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 23, 2005 1:41 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
urmumsacow wrote:
They are only on episode 135, it would take a year to get past the 200 mark. If it matches 300+ it might pull down manga releases.

Looks like you pass. =)


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group