G'day JDoolin,
Welcome to the monastery.
Given your two examples in subsequent posts in this thread, this does what you want:
#!/usr/bin/env perl -l use strict; use warnings; my @test_strings = ( 'a { b } c ( d ) e', 'a { b } c ( d ) e {{F}} ((G))' ); my %replacement_for = ( '{' => ' lbracket ', ' { ' => ' lbracket ', '}' => ' rbracket ', ' } ' => ' rbracket ', ); for (@test_strings) { print "Initial string: '$_'"; replace(); print "Replaced string: '$_'"; } sub replace { s/( { | } |{(?!{)|(?<!})})/$replacement_for{$1}/g; }
Output:
Initial string: 'a { b } c ( d ) e' Replaced string: 'a lbracket b rbracket c ( d ) e' Initial string: 'a { b } c ( d ) e {{F}} ((G))' Replaced string: 'a lbracket b rbracket c ( d ) e { lbracket F rbracke +t } ((G))'
Two things to note:
-- Ken
In reply to Re: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)
by kcott
in thread Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)
by JDoolin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |