On Jul 26, Paul Tremblay said:

>Is there a quicker way to substitute an item in a line than reading the
>line in each time?
>
>I am writing a script to convert RTF to XML. One part of the script
>involves simple substitution, like this:
>
>s/\\ldblquote /<rt_quote\/>/g;
>s/\\rdblquote /<lt_quote\/>/g;
>s/\\emdash /<em_dash\/>/g;
>s/\\rquote /<r_quote\/>/g;
>s/\\tab /<tab\/>/g;
>s/\\lquote /<l_quote\/>/g;

It's best to come up with a hash of strings and replacements:

  my %rep = qw(
    ldblquote   rt_quote
    rdblquote   lt_quote
    emdash      em_dash
    rquote      r_quote
    tab         tab
    lquote      l_quote
  );

Then create a regex:

  my $rx = join "|", map quotemeta, keys %rep;

Then use it in a larger regex:

  $source =~ s[\\($rx) ][<$rep{$1}/>]g;

Ta da!  ONLY one pass through the string.  You'll need to beef up the hash
and the regex as needed, if not everything is '\\IN ' and not every
replacement is '<OUT/>'.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to