Regular Expressions

ProviaFan

Lifer
Mar 17, 2001
14,993
1
0
I've done some poking around on google, and have found tutorials to teach the basics of using regular expressions, but nothing I've found really goes in depth. I'd also like to learn about the history of the "language" - something that hasn't been covered in any of the pages that I've found. Something I read somewhere seemed to indicate that they were invented by a mathematician, but I'd like a little more info than that. So, that's why I would like to ask the UNIX gurus here if they could recommend any books on the topic that could help me get started. Any help / shoves-in-the-right-direction would be appreciated.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
There is no better book than Mastering Regular Expressions, 2nd Edition. You didn't indicate which implementation you wish to use; however, this book covers many "flavors", and languages/tools (Perl, Java, .NET).

I've read both editions, and it is hands down the best. Of course, that's just my opinion, but I'm sure you'll find others agree...
 

ProviaFan

Lifer
Mar 17, 2001
14,993
1
0
Thanks for the advice; I'll be sure to consider that book, as it appears to be just what I'm looking for. I am planning to initially work with sed, but eventually move to more complex tasks with PHP and / or Perl. Java isn't really my cup of tea (no pun intended), and Microsoft's proprietary languages don't really float my boat, either. ;)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
sed can be useful, but why not just do Perl instead? Most regular expressions engines are actually derived from Perl's engine (Java and .NET included), so learning to use regular expressions in Perl would give you greater ability to use your knowledge in other tools/languages. I'm not even sure what kind of an engine sed uses :)

You could do a lot of the same things in perl as you'd do with sed. For example, if you wanted to extract only lines from a file that started with a "#" (for a comment in Perl), you could do:

cat yourfile | perl -ne "print if /^#/;"

It might be a bit of a larger learning curve, but it will be worth it. Of course, you could do a lot of the same things in grep, egrep, etc., but again, they implement an engine that is not widely used in different tools. I try to learn things that will have the best scalability.

Good luck. Knowledge of regular expresssions is very valuable knowledge indeed.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
death@incise ~ $ lynx -dump 'http://forums.anandtech.com/messageview.cfm?catid=33&threadid=944365' | grep 'Title' | sed -e 's/^ *Thread //' | sed -e 's/^T/The t/' | sed -e 's/: / is "/' | sed -e 's/$/", because regular expression rock :D/'

The title is "Regular Expressions", because regular expression rock :D