Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Expanded test case with wrong markup to be ignored

  • markup inside word
  • wrong pairs like *fail_

> to call tf() recursively in the /e evaled replacement part

seems to work well, see sub rec

use strict; use warnings; use Data::Dump qw/pp dd/; use Test::More; my $wiki = '_/one *two*/ th/ree_ null _/f*ur *five*/ six_ null _/se_ven *eig +ht*/ nine_ *fail_'; my $expected = '<u><i>one <b>two</b></i> th/ree</u> null <u><i>f*ur <b>five</b></ +i> six</u> null <u><i>se_ven <b>eight</b></i> nine</u> *fail_'; my $pre = qr/(^|\s|>)/; my $post = qr/($|\s|<)/; my %h = ( '*' => 'b' , '/' => 'i' , '_' => 'u' , ); sub tf { s{ $pre ([_*/]) (.*?) \2 (?=$post)}{$1<$h{$2}>$3</$h{$2}>}xg +}; $_=$wiki; my $DBG = 0; diag "IN <= '$wiki'\n\n" if $DBG; for my $i (1..3) { tf(); diag "$i: '$_'\n\n" if $DBG; } is($_,$expected,"repeated replace"); my $rec_level=0; sub rec { my ($txt) = @_; my $DBG = 1; diag ++$rec_level ."< $txt" if $DBG; $txt =~ s{ $pre ([_*/]) (.*?) \2 (?=$post) }{ my $tag = $h{$2}; "$1<$tag>" . rec($3). "</$tag>" }xge; diag $rec_level-- . "> $txt\n" if $DBG; return $txt; } my $got_rec = rec($wiki); is($got_rec,$expected,"recursive replace"); done_testing;

ok 1 - repeated replace
# 1< _/one *two*/ th/ree_ null _/f*ur *five*/ six_  null _/se_ven *eight*/ nine_ *fail_
# 2< /one *two*/ th/ree
# 3< one *two*
# 4< two
# 4> two
# 3> one two
# 2> one two th/ree
# 2< /f*ur *five*/ six
# 3< f*ur *five*
# 4< five
# 4> five
# 3> f*ur five
# 2> f*ur five six
# 2< /se_ven *eight*/ nine
# 3< se_ven *eight*
# 4< eight
# 4> eight
# 3> se_ven eight
# 2> se_ven eight nine
# 1> one two th/ree null f*ur five six  null se_ven eight nine *fail_
ok 2 - recursive replace

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

In reply to Re: wiki regex reprocessing replacement (UPDATED^2) by LanX
in thread wiki regex reprocessing replacement by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2021-04-13 20:57 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found