I think you expect
perl -e'
$_ = qq{...\n}
.qq{<a href="foo">foo</a>\n}
.qq{<a href="bar">bar</a>\n}
.qq{...\n};
s!(<a href=")(.*?)(">bar</a>)!$1\[$2]$3!s;
print;
'
to output
...
<a href="foo">foo</a>
<a href="[bar]">bar</a>
...
but that's wrong. It outputs
...
<a href="[foo">foo</a>
<a href="bar]">bar</a>
...
The pattern says to match
- Match the start of the string,
- followed by as few characters as possible (implicit leading /.*?/),
- followed by the string '<a href="',
- followed by as few characters as possible,
- followed by the string '">bar</a>'.
Keeping in mind that "as few characters as possible" is zero characters, let's check if the string matches:
- Starting at the begining of the string,
- Do 0 characters follow? Yes, so try to match the next atom.
- Does the string '<a href="' follow? No, so backtrack.
- Does 1 character follow? Yes, so try to match the next atom.
- Does the string '<a href="' follow? No, so backtrack.
- Do 2 characters follow? Yes, so try to match the next atom.
- Does the string '<a href="' follow? No, so backtrack.
- ...
- Do 4 characters follow? Yes, so try to match the next atom.
- Does the string '<a href="' follow? Yes, so try to match the next atom.
- Do 0 characters follow? Yes, so try to match the next atom.
- Does the string '">bar</a>' follow? No, so backtrack.
- Does 1 character follow? Yes, so try to match the next atom.
- Does the string '">bar</a>' follow? No, so backtrack.
- Do 2 characters follow? Yes, so try to match the next atom.
- Does the string '">bar</a>' follow? No, so backtrack.
- ...
- Do 25 characters follow? Yes, so try to match the next atom.
- Does the string '">bar</a>' follow? Yes, so try to match the next atom.
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.