Regular Expression Recipes 258
Regular Expression Recipes: A Problem-Solution Approach | |
author | Nathan A. Good |
pages | 289 |
publisher | Apress |
rating | 8/10 |
reviewer | Raymond Lodato (rlodato AT yahoo DOT com) |
ISBN | 159059441X |
summary | A cookbook of useful regular expressions for Perl, Python and more. |
Regular expressions are not restricted to just the Perl or shell environments, so Nathan offers variations for Python, PHP, and VIM as well. In most cases the translation is relatively straight-forward, but in a few cases a different environment may have (or lack) additional facilities, prompting a different expression to do the same task.
Before you even read chapter 1, Nathan provides a quick summary course on regular expressions, with detail given to each of the five environments you might utilize. He has written the syntax overview in a highly-readable format, making it easy to understand the gobbledy-gook of the most bizarre concoctions you might encounter.
The first chapter (Words and Text) starts simply enough. He gives examples of how to find single words, multiple words, and repeated words, along with examples of how to replace various detected strings with others. In each case he gives an example of its use for each platform, followed by a bit-by-bit breakdown of how it works. Not every environment is given on every example, and in many cases the "How It Works" section refers to the first one, as most REs are identical between the platforms.
The next chapter (URLs and Paths) offers various methods of doing commonly needed parsing. Pulling out file names, query strings, and directories, as well as reconstructing them in useful fashions is covered in the 15 offerings given here. Validating, converting, and extracting fields of CSV and tab-delimited files are handled in chapter 3, while chapter 4 is concerned with validating field formats, as well as re-formatting text for the fields. Chapter 5 handles similar tasks for HTML and XML documents. The final chapter covers expressions that facilitate the management of program code, log files, and the output of selected commands.
First, I must admit that there are a number of useful solutions provided, especially for someone who is concerned with application and web development. However, I did feel a little cheated by the fact that several chapters covered essentially the same task, with only minor variations. It almost seemed as though the author was trying to pad out the solution count to the magic number 100. A simple example: three solutions in chapter one cover (a) replacing smart quotes with straight quotes, (b) replacing copyright symbols with the (c) tri-graph, and (c) replacing trademark symbols with the (tm) sequence. In each case, the expression was simply "s/\xhh/ rep /g;". Did we really need three separate chapters for that? I don't think so.
Another quibble revolves around some of the coding of the expressions. Nathan has made liberal use of the non-capturing groups (that is, (: expr )) to insure only the items that needed replacement were captured. While a worthy idea, in some cases the expression may have been simplified for understanding. Another issue is a slight error in searching for letters. In a number of expressions, Nathan uses [A-z] to capture all letters. Unfortunately, the special characters [, \, ], ^, _, and ` occur between upper-case Z and lower-case a, making it match too much. Either [[:alpha:]] or [A-Za-z] should have been used.
Despite these quibbles, Regular Expression Recipes does provide a useful compendium of solutions for common problems developers face. Presenting the information in a cookbook fashion, along with ensuring that those using something other than Perl don't have to sweat translating the expressions to their target language, makes this a handy book to have. I wouldn't hesitate to recommend it.
You can purchase Regular Expression Recipes: A Problem-Solution Approach from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
cant get used to them (Score:1, Informative)
Points (Score:5, Informative)
1. the binding broke
2. the index has a lot of typos.
Regular expressions in a cookbook? (Score:5, Informative)
Sounds like good eating.
Regular expressions are great, but once you know them and you think you can conquer the world, I find they occasionally let you down. The text editor I was using had a rudementary regular expression search that did not support non-greedy matching. I found that writing a regular expression that finds C style /* comments */ to be quite tricky with only greeding matching [ostermiller.org]. I wrote it up as an article where I build the expression piece by piece showing common things you might try that won't work.
If you want more of a challenge, try writing a regular expression that find any <script></script> tags along with anything in between using only greedy matching. You will find that the length of your regular expression goes up exponentially with the length of your ending condition.
--
Calculator for Converting Currency [ostermiller.org]
I personally... (Score:5, Informative)
add this book to your list (Score:3, Informative)
While I can't vouch for the quality of the reviewed book,if you want something definitive on regular expressions, Mastering Regular Expressions, Second Edition [amazon.com] by Jeffrey E. F. Friedl is an absolute must for your professional library. Jeffrey breaks down and then builds back up what regular expressions are and how they work, and offers an entire matrix breakout of the slightly different implementations among the most common utilities (grep, sed, awk, perl...). Not to shill for amazon, but if you select the reviewed book, the "buy this book too, and you get this great price" deal actually includes the Mastering Regular Expressions, Second Edition. . Get 'em both, you won't be sorry.
Re:A language in their own right. (Score:2, Informative)
Re:A language in their own right. (Score:2, Informative)
Re:A language in their own right. (Score:4, Informative)
Don't you just love to sound like a StarTrek character, with all that fancy terminology?
Go look up your complexity book - if you have one - regexes are not even close to Turing-complete.
Re:Regular expressions in a cookbook? (Score:5, Informative)
Re:Unacceptable mistakes (Score:3, Informative)
Regexes are overused (Score:5, Informative)
Regexes are actually a pretty poor way to extract information from comma-delimited or tab-delimited files, for example. By the time you're done dealing with escaped commas, escaped tabs, quoting characters (which many CSV and TDT exporters use in addition to commas and tabs), escaped quote characters, escaped newlines, and escaped escape chars, you end up with a super-complicated regex.
HTML is even more complicated. You have HTML comments and nested tags on top of everything else.
To validate a simple email address, Jeffrey Friedl in his Mastering Regular Expressions book for O'Reilly writes an *11-page* regex.
Most of the time the correct answer is not "here is a regex recipe" but rather "here is a simple library to do the job property with a parser", like Text::CSV or HTML::Parser in perl.
F*ck this book and all others like it: (Score:1, Informative)
Regex Coach helps building Regexp (Score:5, Informative)
This program assists you building regular expressions. I've never used it (real men code regexp at once and it works). But some friends recommend it.
Re:Regexes are overused (Score:2, Informative)
For some sort of system that processes umpteen billion transactions per second, they can be a godsend. For parsing a
It's all about knowing how and when to use the tool. A pneumatic nailgun can save a carpenter hours on a jobsite, but it's a waste of time to set it all up if you only need to knock in one nailhead that's popped through the drywall.
Re:cant get used to them (Score:2, Informative)
I have a suggestion. Write a few regular expressions to get your brain refreshed on them, then go read this excellent article [plover.com] on how regular expressions work. At the very least, it will clear some confusing things up. Most likely you'll find that having a better understanding of the underlying concepts will make it easier for you to work with regular expressions day to day.
Also, it helps if you are familiar with finite state machines. I learned about them in a couple classes while getting my CS degree, but they're not that hard and most people should be able to grasp them without any kind of formal CS training.
Re:cant get used to them (Score:2, Informative)
They may be kind of hard to get used to, but not has hard as writing, debugging and maintaining a dozen or more lines of custom string parsing code for each case where you would use one.
Re:cant get used to them (Score:4, Informative)
Different flavors? (Score:4, Informative)
My understanding is that even the UNIX world sports several different flavors of regular expression in grep, egrep, fgrep, etc.
The biggest barrier to _my_ use of regular expressions is that every time I switch from one regular expression context to another, it takes me a good half hour to refresh my memory of what does and doesn't work in each environment.
Re:Regexes are overused (Score:3, Informative)
You get tokenizer, regex, and a parser library (spirit), in sorted by increasing caliber.
It's all about the right tool for the job.
Re:Regular expressions in a cookbook? (Score:3, Informative)
Your expression fails for this case:
<script><scri</script>
It will match <scri< with your |</scri[^p] rule and then go on to match beyond the end of your regular expression.But I acknowledge that it may be quadratic rather than exponenetial even with a correct regular expression.
--
Exchange Rate Calculator [ostermiller.org]
Re:Unacceptable mistakes (Score:3, Informative)
Free Alternative (Score:4, Informative)
http://www.regexlib.com/ [regexlib.com]
Re:Another one? (Score:4, Informative)
Re:Unacceptable mistakes (Score:2, Informative)
Indeed. If anyone is interested in why ASCII sticks a few characters in there, it's because it allows you to flip a bit to switch between cases.
Re:Regex Coach helps building Regexp (Score:2, Informative)
Re:cant get used to them (Score:4, Informative)
ignorance is bliss (Score:3, Informative)
duh! Repeat after me: HTML is not a regular language. There is no regular expression that can match it. The problem arises when people try to use regular expressions without understanding what they are. But, as the saying goes, when the only tool you have is a hammer, everything looks like a nail...
Re:Regexes How2 (Score:5, Informative)
http://www.weitz.de/regex-coach/
It is a very very nice interactive pgm that lets you debug REGEXES on the fly visually, by feeding them sample text.
Re:ignorance is bliss (Score:3, Informative)
Script tags cannot be nested which makes that portion of html able to be matched by a regular expression.
--
Currency conversion calculator [ostermiller.org]
Re:I personally... (Score:3, Informative)
I'm one of the few people who doesn't like Friedl's
Regular Expressions/. (I have the first edition.)
First, he says that extended regexp engines, such as Perl's, use
nondeterministic finite automata (NFA). Not true; NFA's can
accept exactly the same languages as DFA's (deterministic finite
automata). The extended regexps use search-and-backtrack
engines.
Friedl gives some examples of (extended) regexps that have
catastrophic worst-case behavior, but doesn't present a
systematic method for recognizing or avoiding them. The naive
use of extended regexps, mostly by people who think they have
mastered them, is setting us up for denial-of-service attacks
based on the worst-case complexity of regular expressions.
Formal regular expressions are exactly the languages DFA's and
NFA's can accept. A DFA can parse any string in time
proportional to the length of the string. Compiling the DFA may
be exponential time, and space, but at least we find out at
compile time, not when some attacker figures out a case we
missed.
:help pattern (Score:4, Informative)
Of course, if you use the one true text editor [vim.org], all you need to know about regular expressions is:
:)