On 1/23/06, Chris Cosner <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have some data coming into a system with markup that I need to
> reinterpret into HTML. I have a feeling I'm making this problem more
> complex than it needs to be.
>
> The beginning and ending markup are the same. So <I>text<I> would
> become <I>text</I> in HTML. A single line can have more than one of
> these italicized words, for example.
>
> So this doesn't work:
> s/(\<I\>)(.*)(\<I\>)/$1$2\<\/I\>/g
> because .* matches anything in between and only the last one is
> considered. <I>text<I>blahblah<I>text<I> becomes
> <I>textblahblahtext</I>.
>
> I would like it to match every second <I> and replace it with </I>, no
> matter how many occurrences in a line. Am I on the right track with
> something like this?:

Just use the "non-greedy" form of "*":

    s{(\<I\>)(.*?)(\<I\>)}{$1$2\<\/I\>}g

should do what you want.

> for $line (@descriptions) {
> ...
> $count = 0;
> $line =~
> s{
>          \<I\>
> }{
>      if (++$count==2){
>          \<\/I\>
>          }
>      else {
>          \<I\>
>          }
> }gex;
> ...
> }
> which gives me the following error:
> Unterminated <> operator at program.pl line 48.
> Line 48 is }{
> So I have the syntax wrong, at the very least.
>
> Should I be thinking more in terms of some sort of lookahead?
>
> Any hints as to how to approach this will be appreciated.
>
> -Chris Cosner

The problem here is that once you use "e", the second part of the
statement is an expression, and needs to evaluate as perl.Your "if"
construction doesn't.

    if ($count == 2) {<I>}

for instance, doen't work. Perl can't execute <I> (which is what ti
seems to think you meant by \<\/I\>) even if it wants to.  If the s///
operator is confusing, think about what would happen if you tried to
do something like:

    my $var = {your code}

The second half of the substitution will get whatever would end of in
the variable if you were assigning to a variable instead of performing
a substitution. You also don't seem to actually increment count
anywhere.

Try something more like

    my $count = 0 ;
    s/\<I\>/ sub {
        $count++;
        my $return;
        if ($count == 2) {
            $count = 0;
            $return = '<\I>';
        } else {
            $return = '<I>';
        }
        return $return;
    } /gxe;
    # untested, but you get the idea:
    # you need to return a value to be substituted

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to