I've recently been reading with interest some of the previous discussions on the use of .* and .*? that are scattered around the Monastery (Death to Dot Star!, Dot star okay, or not? and Ovid, Long Live .*? (dot star question-mark), among others). These have gone into why .* and its friends are considered bad, and I think I understand the reasoning behind this point of view.
I've recently had to write some code at work, though, which got me thinking about this. The code is simple enough - it parses some XML tags to grab data from a file. (Aside: See Production Environments and "Foreign" Code for why I can't just use the XML modules, which I'd much rather do). The code, however, implements a regex to grab the data from the file - and uses as part of this the dreaded .*, albeit in a non-greedy fashion.
I've thought about this long and hard, and I don't think that I can see a straightforward, easy-to-read way of implementing the same code without the .*?, for which the regex I wrote and an example are below.
I've considered using character classes and look-aheads to pull the data between the two XML tags (which can include a wide and interesting array of alphanumeric and other characters), but I can't see how these would be either beneficial or efficient for a large set of data.my $example = "<ClientID type="String">A1234BX</ClientID>"; $example =~ /^\s*\<(\w+)\s[\w\"\=]+\>(.*?)\<\//; my $tag = $1; my $data = $2; # do something with the data
I guess I'm interested to know what the general consensus for the use of .* is. Is it something to be avoided at all costs, or is it a powerful, oft-misused tool that can be useful and beneficial in carefully controlled circumstances?
While I'm at it *grin*, does anyone have a "better idea" for pulling the data out of the tags? Would this count as an acceptable exception to the "Don't Use Dot Star" rule that seems to be prevalent throughout the Monastery?
Any opinions, suggestions and comments are welcome :)
-- Foxcub
#include www.liquidfusion.org.uk
In reply to An "ethical" use of dot-star ..? by Tanalis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |