We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › regular expressions and evil syntax!
Page Index Toggle Pages: 1
regular expressions and evil syntax! (Read 571 times)
regular expressions and evil syntax!
Aug 13th, 2008, 9:35am
 
Hi

I am trying to sort some data using regex, I thought it
was time to learn this mystical syntax.

I am having serious trouble, to say the least.

The below example is inspired of the way Ben Fry does it in the "Visualizing Data" ... but it does not get any other pattern than a perfect match e.g. "123" = "123"

Here Im trying to get the date out, but my goal is to seperate the date, time, start, history and duration out in one go.

Regards Ricki
Quote:


void setup()
{  
 size(200, 200);
 background(0);
 parseData();  
}

void parseData()
{
 String record = "[#date: ""26-06-2008"", #start: ""14:10:52"", #history: [21: 1, 31: 6], #duration: 11]";
 Pattern date = Pattern.compile("([0-9]{2}\\-[0-9]{2}\\-2008)");
 //Pattern time = Pattern.compile("([0-9]{1,2}\\:[0-9]{2}\\:[0-9]{2})");
 Matcher m = date.matcher(record);

 if(m.matches())
 {
    println("hit!");
    println(m.group(1));
 }
}



Re: regular expressions and evil syntax!
Reply #1 - Aug 13th, 2008, 10:00am
 
http://java.sun.com/docs/books/tutorial/essential/regex/

Code:

void setup()
{
size(200, 200);
background(0);
parseData();
}

void parseData()
{
String record = "[#date: \"26-06-2008\", #start: \"14:10:52\", #history: [21: 1, 31: 6], #duration: 11]";
Pattern date = Pattern.compile(".*\\\"([0-9]{2})-([0-9]{2})-(200[0-9]{1})\\\".*");
Matcher m = date.matcher(record);

if(m.matches())
{
println("hit!");
println(m.group(1));
}
}


F
Re: regular expressions and evil syntax!
Reply #2 - Aug 13th, 2008, 4:50pm
 
also try the new match() function, which removes some of the additional syntax:
http://processing.org/reference/match_.html

and since you're doing time/date information, you can also use DateFormat (covered in the book as well, see p. 246) which will parse it into a Date object.
Re: regular expressions and evil syntax!
Reply #3 - Aug 13th, 2008, 6:06pm
 
Thank you Florian and Ben, that was a life saver.

Ben: Sadly work has me lingering on page 157 for 2 days straight now :/
(inspiring and great book!)
I don't really need the date time values for much other than drawing a small graph in Processing, so I´ll take that hurdle when it comes up in the book :)

Florian: Thanks for the link, I had been there before but had a hard time getting the syntax to compile in Processing, but your example helped out with that part.

I implemented the rest of the values with the logic of Florians example
but here is, especially for my own amusement, a short run through I made from RTM ;)
after dissecting the example, for others having trouble.

compile(".*\\\"([0-9]{2}-[0-9]{2}-200[0-9]{1})\\\".*")

" start the expression:
. any character except line breaks
* 0 or more of the previous characters in the string.
So .* means - we don't care what ever comes before in this string.

\\\ escape characters for the \ in the string, as it is in the original string:
" as it is in the original string, i.e. this is part of the string not the regex.

( start of a group, meaning: if the regex finds a match, then the part of the string incapsulated with (string here) will be stored in m.group(1);

[0-9]{2} means, any number and the {2} concatenated on the end of the expression means any number of 2 characters i size. e.g. 12 or 45, i other words 00-99.

- part of the string, not the regex.

[0-9]{2} the same as the previous.

-200[0-9]{1} means the string must contain 200x where the x is a number between 0 and 9 (the script will only go up to the year 2009, could be
written -[0-9]{4} to cover the year 0 to 9999 ... but hey, the data is all ready there and only involves 2007 and 2008.

\\\ escape characters for the \" and it ends with:

.* meaning, we don't care what ever comes after this part.


The finished script looks like this:

Quote:


/* this is how the values in the loaded text looks like
[#date: "27-06-2008", #start: "14:10:52", #history: [21: 1, 31: 6], #duration: 11]
*/

String[] records;
String[] dateArray;
String[] timeArray;
String[] durationArray;


void setup()  
{    
 size(200, 200);  
 background(0);  
 records = loadStrings("staticticsWave.txt");
 parseData(records);    

}  
 
void parseData(String[] records)  
{  
 dateArray = new String[records.length];
 timeArray = new String[records.length];  
 durationArray = new String[records.length];

 Pattern date = Pattern.compile(".*([0-9]{2}-[0-9]{2}-200[0-9]{1}).*([0-9]{2}:[0-9]{2}:[0-9]{2}).*#duration:\\s([0-9]{0,}).*");
 for(int i = 0; i < records.length; i++)
 {
   Matcher m = date.matcher(records[i]);    
   if(m.matches())  
   {  
     dateArray[i] = m.group(1);
     timeArray[i] = m.group(2);
     durationArray[i] = m.group(3);
     
     println(i + ":   Date: " + dateArray[i] + " - Time: " + timeArray[i] + " - Duration: " + durationArray[i]);  
   }  
 }
}




Thanks again guys for the quick help !

Ricki G
Re: regular expressions and evil syntax!
Reply #4 - Aug 15th, 2008, 8:08am
 
If you ever have time, a good book on regular expressions either for straight read or as bookshelf reference:

http://books.google.com/books?id=ucwR4KIvExMC&dq=mastering+regular+expression&pg=PP1&ots=QLuGt21SOl&sig=WA8-nJhdfQlk6Q_LJV5S7ad_yoI&hl=en&sa=X&oi=book_result&resnum=1&ct=result

Processing tasks will generally not involve heavy regular expression usage, but in general computing, regular expressions have enormous utility. When integrated directly into an editor like emacs or vi, they give you a lot of development utility as well.
Re: regular expressions and evil syntax!
Reply #5 - Aug 15th, 2008, 11:56am
 
Thanks Smith

That book does look quite tempting..
and I think I'll be using more time with regex' in the future. (the Boss has
all ready started throwing large data files on my desk).

How come you write that Processing tasks generally will not involve
regex usage?

Is there something I should know?
I can get to sorting my large data files with a minimum of code and in mere minutes with Processing, a task that takes hours in other languages ... and I assume that there are no speed or API issues with using Processing over C, C#, Java or actionScript?

Or did you just mean that often Processing is used for other things and I got all worried for no reason Smiley
Re: regular expressions and evil syntax!
Reply #6 - Aug 16th, 2008, 5:48am
 
Most people use Processing for visualization, video and audio processing, and control. These tasks do not generally require that you do a lot of string processing. Regular expressions concern themselves entirely with string processing.

True, when visualizing data sets, the data sets may come with lots of strings on which you have to perform regular expressions to select out the data you need for your visualization code. However, unless dealing with a dynamic stream, ideally you clean up the data outside of Processing so that your sketch does not have to do regular expressions on the fly. The power of regular expressions does come at a significant performance cost.

That said, never attempt to pre-optimize code. If doing regular expression processing in real time works for your sketch, do not spend time trying to make the process more efficient than you need.

Processing has as much capability as any other language in performing regular expressions. In fact, when you write Processing code, you write Java code, because the Processing authors implemented processing in Java and the Processing Desktop Environment (itself a Java application) mostly takes your Java code and sticks it in a pre-assembled Java class. Processing then executes it using a Java Virtual Machine (JVM). Java lacked regular expressions for a long time, but more recent versions of Java come with a fairly exhaustive regular expression functionality.

So don't fret. Processing and Java can perform any computation any other language can (we call it "Turing complete" - http://en.wikipedia.org/wiki/Turing_completeness). If Processing works for your needs, go for it. You will not have wasted time learning anything useless.
Page Index Toggle Pages: 1