in reply to RegEx Help
For starters, Perl and CPAN will help you solve your puzzle:
prints, in relevant part,:use YAPE::Regex::Explain; say YAPE::Regex::Explain->new( qr{/(\d.*?)/i} )->explain;
---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \1
On other words, at lines 11 and 24, you'll be better off with a simple "\d+" to deal with any number of digits, before moving on to other possibilities.
Semi-OT NOTE: YAPE::Regex's use of "digits" plural ! for a single "\d" seems misleading, to me. Yes, of course the next char, matched by the dot, could be a digit, but that explanation is obscured by the use of the plural in the explanation of \d).
Here's a very loose extrapolation/translation of YAPE::Regex's output in language I think is actually more precise:
---------------------------------------- \d one digit <br> ---------------------------------------- .*? anything (except a newline), with as few as zero instances (times) of anything ...and absolutely no more than (in the case of yo +ur sample data) 'one additional digit'." ----------------------------------------
When the best tools you know how to use come up short, you can use a few print statements for debugging many problems -- t'ain't necessarily the best way, but you might find illumination were you to insert just two such:
Obligatory observation: you're not using strict and warnings. You should, at least until you know Perl so well that their assistance is no longer needed to write code that (usually) does what you want and expect... and so well that you know when you can save yourself the line(s) of code.
In case you're wondering at the much-belated and possibly-redundant post, this node has been a work in progress for many hours -- interspersed with Fire calls (2 each), SWMBO's decision (1 each) that this was the day to select and cut an Xmas tree, and loading/delivering a cord of wood (1 each, $350/cord) with a friend... Probably not much point in saying more; maybe not even in this, at this late date.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: RegEx Help
by Anonymous Monk on Dec 20, 2011 at 14:54 UTC | |
by ww (Archbishop) on Dec 20, 2011 at 17:21 UTC |