Owner: paul
Members: 23




 
Regular Expressions - 16 February, 2007
LearningNerd says
Are you familiar with these? Do you use them a lot? Do you like them, love them, hate them? Were they difficult to learn?

I'm learning PHP and I came across regular expressions, having absolutely no idea what they were. I have an idea now, but I was hoping I could get the basics of it -- hopefully by today, so I can make them the subject of my next video tutorial-blog thingy.

So general discussion about them and any tips or helpful links would be much appreciated!
Total Topic Karma: 43 - More by this Author
paul says
+1 Karma
I've been doing php for like 4 years or more now, and i still don't get it.. lol.
- 16 February, 2007
Nadeem says
+3 Karma
I've used them a bit, but never really had to do anything particularly complex in it, so I don't know any of the more advanced stuff.

There's an interesting tutorial of them here. The APIs they describe are the Java and C++ ones, and the context is algorithm programming contests, but you only need to pay attention to the basic fundamentals - character classes, quantifiers and what not - since that's pretty much language independent. Once you know that, I think you'll be pretty well placed to specialize what you know for whatever language you're interested in. Even PHP, which I have no clue about.

The article also has a bunch of links at the end which you might find interesting.
- 16 February, 2007
LearningNerd says
+2 Karma
Thanks for that link, Nadeem! I'll look through it. And Paul, thanks... that's very reassuring, lol.
- 16 February, 2007
Rob Masson says
+3 Karma
LN,

O'reilly has a great book called Mastering Regular Expressions which I hightly recommend. Regex is an incredibly useful thing to know because it pops up a lot in any text processing application and Web Programming is very text processing heavy. You also get to use it with funcitonal programming languages like XSLT and it allows you to transform XML from one scheme to another very easily... But it is not necessarily very intuitive. Part of it is that it is based on some ptryy cryptic symbols for concepts like end of Line, Beginnig of Line, First Occurence, etc. It also forces you to think of a text stream from many directions in some cases..

Overall it is a very Uber Geek thing to have in your toolbox..

Rob

PS.. Get the book and look like a hero! *grin*
- 16 February, 2007
LearningNerd says
+2 Karma
Mastering Regular Expressions... O'Reilly... Too bad Valentine's Day is already over. Maybe I'll ask for it for my next birthday.
- 16 February, 2007
Cappy says
+3 Karma
I've known PHP for a while but I didn't learn Regular Expression until I learned Perl recently.

I used these two sites to help me:
http://en.kerouac3001.com/regex-tutorial-8.htm
http://www.cs.tut.fi/~jkorpela/perl/regexp.html
- 17 February, 2007
Rob Masson says
+1 Karma
Cappy.. Great sites! Thanks for the links man!
- 17 February, 2007
(Guest) Guest says
+1 Karma
I haven't used with PHP. In my job I do a lot of searching with grep, so Regex is a fantastic aid to work with the right files and match the patterns I seek. At a previous job, I did quite bit of Perl hacking, that is where I picked up my first exposure.

I second the Mastering Regular Expressions book from O'Reilly. Great book.
- 17 February, 2007
eviljawdy says
+1 Karma
Regular Expressions are neither regular, nor an expression... unless you're using it to describe the expression on your face when you get kicked in the nads?
/?s//n\\/&aaklehf;
- 18 February, 2007
eviljawdy says
+2 Karma
ah crap, it did it again, cut off half my post - I think I know why now. Anyway, what I said was, that I used to use RegEx's for spam filter rules in our Barracuda Spam Firewall.
But when you get obscenely complex regex's, it just looks daft!
- 18 February, 2007
Rob Masson says
+4 Karma
EvilJ,

You are right, maintaining Regex code can be nightmarinsh.. like Perl, it is often easier to simply solve the problem again from scratch rather than try and decode and debug a current solution and try and modify it. That is an inherent problem with functional programming idioms like that. Because the final result is the collective amalgam of the complete expressions behavior and elements within the expression affect each other, it can be obscenely difficult to predict the exact outcome.

It is however a great way to become indispensible at work.. *grin*
- 20 February, 2007
DKlined says
+2 Karma
Learning Regex is really a pain. I've known PHP for atleast 4 years now and I stayed away from Regex because it is a pain to understand. But lately I got into complex websites and regex is a godsend. It is hard to learn. I looked over many tutorials, but it took trial and error for me to really learn it.
- 20 February, 2007
Rob Masson says
+3 Karma
DKlined,

It is hard to get your head around because it has a different programming model. You essentially tell the regex parser WHat you want but not How to do it, which is fundamentally different than regular procedural programming.

Once you get your head wrapped around it, it is a very powerful tool as you clearly have discovered.
- 20 February, 2007
LearningNerd says
+3 Karma
Thanks for all the replies, everyone -- and thanks for those links, Cappy! I found a bunch of other sites after doing some research, and now I have a pretty good understanding of the basic basics.

The fruits of my labor: http://programming.learningnerd.com/php-day-22

A couple notable links I found are the regular expression library (http://regexlib.com/) and what's probably the longest regular expression ever made (http://comox.textdrive.com/pipermail/wp-hackers/2005-March/000268.html). That thing is SCARY.
- 21 February, 2007
Rob Masson says
+2 Karma
LN,

Wow GREAT tutorial and Fantastic site! I'm hooked! *grin*

- 21 February, 2007
Cappy says
+3 Karma
It's a woman on the interweb! Quick, grab your stalking tools!

Very nice tutorial though! =)
I also watched your short tutorial on $_GET, $_POST, and $_REQUEST and that was good too! As a funny story, when I first learned PHP I learned from a tutorial that didn't go into that. I applied for a pretty simple PHP job and I didn't get it, simply because I didn't use GET/POST. I did declare all my variables so there could be no code injection and it would have been trivial to add a POST onto the front of the code but they thought I was too young, I guess. The lesson here is that most free guides on the Internet suck and you learn incorrect information. In my mind, a book is always worth its weight in platinum (if you read it).
Today I was reading on Slashdot about how Perl is losing popularity to Python because the Perl functions are available separately as sed/awk/grep and Python has better text manipulation. In researching sed I found a neat tutorial.
http://www.grymoire.com/Unix/Sed.html

I can type into my [linux] command line and use this for piping information through it! I'll show you =)

cappy@cappychan:~$ echo "Cappy is awesome" | sed -e 's/awesome/awesome and he talks too much sometimes/' -e 's/sometimes/sometimes but that is what makes him adorable/'

Cappy is awesome and he talks too much sometimes but that is what makes him adorable

(Obviously, it would be easier just to append the strings together but it just shows the idea)

Using Regular Expressions on the command line is just fantastic. Using RegEx it is trivial to do tasks such as mass file renaming, file moving, file searching, really any kind of "data mining". I would put it down as one of the most powerful programming concepts that you can use =)
- 23 February, 2007
Rob Masson says
+3 Karma
Cappy,

Great link! You are right about the sometimes dubious quality / completeness of information on the Internet. I am a hard core technical book collector for this very reason and I swear by O'Reilly because of it's high standards proven to me personally time after time. But Liz/LN I have to say you really have gone above and beyond in your tutorials and I love the way you present it in a very simple "whiteboard" with voice over.. It's elegant and compelling! Very nice!

Cappy I agree with you about the power of piping. It saddens me that so many developers have grown up in a windows world and do not understadn the power of tools like sed and grep and awk and do nut understand how piping can make them into programming juggernaughts! I have a copy of cygwin installed on all my machines for this very purpose and always seem to amaze someone at the power these tools offer. Regular Expressions are just such a compellingly powerful idea and I just wich more poeple would spend the time to learn them.

- 23 February, 2007
LearningNerd says
+2 Karma
Yeah, I don't know the first thing about any of that stuff. It does sound cool, though. I'll have to bookmark that link.

As for online tutorials, I have to agree, there's a lot of incorrect information out there -- and a lot of bad writing... *cringe* But there's some good stuff, too. Of course, it takes a lot more time to find it and check to see if the information is right than if you just bought a book. But I'm cheap like that, lol. Plus I said I could learn anything for free from the internet, so I need to test my theory. =P
- 23 February, 2007
Rob Masson says
+2 Karma
Ln.. piping and scripting in Linux / Unix could be your next topic! *grin* Actually leanring sed or awk would be a great follow up to learning about regex!
- 23 February, 2007
Comment:

Name: