http://www.perlmonks.org?node_id=922664


in reply to String replace, another question

I don't understand the question, I'm blinded by the long lines and can' find the problems.

You can use just about any characters as delimiters in s///, you don't have to use the slash. By using something else, say matching curly braxes, you don't have to excape all those slashes that are part of the text. And you don't have to searcxh character by character to find the separation between 'search' and 'replace'.

As well, if you want to specify 5 spaces, you can use \s{5} rather than \s\s\s\s\s. It means you don't have to count; you KNOW the pattern is looking for 5 of them. Ooops, there's actually 6 of them....proves my point. It doesn't matter that braces are nested, cause braces are balanced.

# Up in the Constants section # Readonly my $NL = "\n"; Readonly my $RETURN = "\r"; Readonly my $SPACE = q{ }; Readonly my $TAB = "\t" my $search_text = qr{<binddn>cn=abcde</binddn> # buttondown $NL # newline $SPACE{6} # 6 spaces <bindpass> # something .* # anything </bindpass> # end something }xms; my $replace_text = "<binddn>cn=abcde</binddn>$RETURN$TAB<bindpass>welc +ome1</bindpass>"; $line =~ s{$search_text} {$replace_text};

As Occam said: Entia non sunt multiplicanda praeter necessitatem.