For your first question, you could avoid the regex match entirely by using substr and index, writing
CHUNK: while ( ( my $pos = pos $sentence ) < length $sentence ) { for my $phrase ( @phrases ) { if ( my $index = index($sentence, $phrase ) >= $pos ) { my $length = length $phrase; substr($sentence, $index + $length, 0, '#'); substr($sentence, $index, 0, '#'); pos $sentence = $index + $length + 2; next CHUNK; } } last CHUNK; }
For the second question, split has no way of knowing what you want the stuff between the separators to be; but you could make the phrases themselves the 'separators', and capture them:
my @split = split /($phrase1|$phrase2)/, $sentence; @phrases = @split[map { 2 * $_ + 1 } 0 .. ($#split - 1)/2];
This works because leading empty fields are preserved, so the separators are in the odd-indexed fields. I'm sure there's a better way to get just the odd-indexed fields, though.

UPDATE: Fixed my code to put # after as well as before. Also, I misunderstood your original question to mean that the regex matching itself was too slow, rather than that making up a bunch of regexes was too slow. kyle's solution is probably faster.


In reply to Re: phrase marking by JadeNB
in thread phrase marking by newbio

Title:
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, 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:
    & &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.