Quote sensitive substitution? 
Author Message
 Quote sensitive substitution?

I have a problem which I thought SHOULD be quite easy in Perl, but the
solution escapes me.  What I want is to be able to do substitution
*except* inside quotes (I only need to support double quotes).

Example:

$_ = 'hello world "hello world"'

s/\bworld\b/universe/g  ---> 'hello universe "hello universe"'

what I want is          ---> 'hello universe "hello world"'

What's the best way to do this in Perl?

Regards,
Brian

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//////////////////////////////////////

\\ Supercomputing Research Center   \/ phone: (301) 805-7322             //
// 17100 Science Drive              /\ quote: WAIT UNTIL clock'event;    \\
\\ Bowie, Maryland  20715           /\        clock <= snooze;           //
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



Sat, 03 May 1997 00:14:49 GMT  
 Quote sensitive substitution?
|> solution escapes me.  What I want is to be able to do substitution
|> *except* inside quotes (I only need to support double quotes).
|>
|> $_ = 'hello world "hello world"'
|> s/\bworld\b/universe/g  ---> 'hello universe "hello universe"'
|>
|> what I want is          ---> 'hello universe "hello world"'

Off the top of my head, try

  $dbl_quote_string = q/"[^"]*"/;
  s/$dbl_quote_string|\bworld\b/$& eq 'world' ? 'universe': $&/oge;

If you're on perl5, you can replace the first line by
  $dbl_quote_string = q/".*?"/;

If you want to allow for escaped characters in the quoted string, a'la

   hello world "he said \"hello world\"." hello world
                ^^^^^^^^^^^^^^^^^^^^^^^^-------string with embedded quotes.

then use (in both perl4 and perl5)
   $dbl_quote_string = q/"([^\\"]|\\[\d\D])*"/;

Of course, that's heinously inefficient with perl's implementation
of regexes, so use

   $dbl_quote_string = q/"[^\\"]*(\\[\d\D][^\\"]*)*"/;

which is functionally the same and a wholehellofalot faster.

Tom, recognize any of this? (-:

BTW, Brian, throw a semicolon at the end of your $_ assignment. (-:
        *jeffrey*
-------------------------------------------------------------------------



Sat, 03 May 1997 19:35:00 GMT  
 Quote sensitive substitution?

Quote:

>I have a problem which I thought SHOULD be quite easy in Perl, but the
>solution escapes me.  What I want is to be able to do substitution
>*except* inside quotes (I only need to support double quotes).

>Example:

>$_ = 'hello world "hello world"'

>s/\bworld\b/universe/g  ---> 'hello universe "hello universe"'

>what I want is          ---> 'hello universe "hello world"'

>What's the best way to do this in Perl?

Dunno about the best way, but here is one way:

-------------------------------------------------
$str = 'hello world "hello world"';

# Split $str into quoted and unquoted parts.  The parens in split pattern

# Now do the substitution on unquoted parts (every other chunk)

  next if $chunk =~ /^"/;
  $chunk =~ s/\bworld\b/universe/g

Quote:
}

# Put things togehter again

print "$str\n";
-------------------------------------------------

If you're sure the quotes are well behaved you may get away with a
single substitution.  The above will even work for things like

'hello world "hello "world"'

where '"hello "world"' is treated as a single quoted chunk.

Anno



Sat, 03 May 1997 19:01:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: context-sensitive substitutions in LaTeX files

2. Global substitutions that leaves quoted text as is

3. replace single quote to double quote with an example

4. replace single quote to double quote

5. changing a single quote string to a double quote string

6. Different Perl quote-er (was Re: A plethora of quote sources (LONGER))

7. performance penalty: bareword, single quoting, double quoting

8. regex to replace quotes delimited by quotes.

9. replace single quote to double quote with an example

10. Converting single quoted string to double quoted string

11. quotes & double quotes in perl CGI

12. Variable Substitution (not string substitution)

 

 
Powered by phpBB® Forum Software