In order for
m/<some stuff>/g to return multiple matches, you need to either evaluate it in a list context or repetitively evaluate the same match in scalar context.
The scalar match can be done with with a while loop, as shown in other replies. Note that the same string must be matched, so if you used
sub foo { $mystring }
while (foo =~ m/blah../g) {
, you would get the first match over and over, since each time you are matching on a different copy of the string.
The list context match can be done with foreach for iteration, e.g.:
$/=undef;
for (<> =~ /\((.*?)\)/g) { print $1, "\n" }
while _repeatedly_ evaluates its condition in scalar context, foreach repeatedly executes its body for values of the list obtained by evaluating the argument _once_ in list context. Btw, this means you do not want to use foreach if your list is way too big for your memory...
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.