Another tricky (at least in my book) regex question, that unfortunately looks deceptively simple. Here's some code...
$test = "abc";
while $test =~ m/(a(b?c)?)/g print "$1\n";
Ultimately, I need the regex to produce the following output:
a
ab
abc
Using greedy quantifiers, I'm currently getting only the single match "abc", and if I switch to using reluctant quantifiers, I only get the single match "a".
Of course, I understand why that's the case. The matcher exits as soon as it has found a match (either greedily or reluctantly) and then quits, without trying to find another match.
But for my application, I really need to find all instances of all matches, even if they occur within other matches.
Now, before you answer the question, I know what a lot of you are going to say: "use $2 and $3".
Unfortunately, that's not going to work. The regular expression isn't hard coded into the application. Instead, it's dynamically generated from a list of (possibly thousands) of search terms. In fact, my real code uses non-capturing groups in the interior of the expression, so the only captured group that I can access is $1.
Any ideas?
--BenjiSmith
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.