in reply to guestbook font [colors]

You actually have a couple of tasks. First split your input up into sentences. That is somewhat non-trivial for a start. However, modules like Lingua::EN::Sentence help solve that problem. Once you have an array of sentences you can process each one by:

use warnings; use strict; my @sentences = ( "This is [color]some text[/color].\n", "This is a [color]test[/color] oops![/color]\n", ); s!\[color\](.*)\[/color\]!\[blue\]$1\[/blue\]!g for @sentences; s!\[/?color\]!!g for @sentences; print for @sentences;

Prints:

This is [blue]some text[/blue]. This is a [blue]test oops![/blue]

Note the second pass to clean up any old markup that got left behind.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: guestbook font [colors]
by Anonymous Monk on Aug 09, 2006 at 04:14 UTC
    How would this work on colors embedded in each other like in my original example? Would this work?

    I don't quite understand the magic in this regex.

    Thank you!

      To get a better answer provide a better example. What does your original text look like and what do you want the new version to look like? I answered my best guess at what you might have. I can't imagine what you might expect something like:

      This is a [color][color]test[/color][/color]

      to be rendered as. There is not enough information in that to indicate how color should be replaced in each case.

      You should visit the Tutorials section, in particular Strings and Pattern Matching, Regular Expressions, and Parsing.


      DWIM is Perl's answer to Gödel
        Okay, in my real code I have a database filled with color codes that are available. All codes have the same open and close tag, with the close having /.

        I'm iterating over my messages in a foreach() loop and splitting the messages up into individual data chunks (data, name, url, and message).

        From here, I want to filter the message for color codes (which I'd probably push into an array.

        my @array = qw(red blue orange); $message =~ s///; # subs all occurences of [red]test[/red] or [blue] w +oot! [/blue].

        A real world look at what the message might be:

        How [red]much would[red] could a wood [blue]huck chuck[blue]. If a [re +d]wood chuch could chuck[/red] wood? All the wood that peter pan is m +ade of.

        The output is pretty self explanatory which would be the real font color="red", color="blue" tags.

        Code tags added by GrandFather