Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: String replace, another question

by TomDLux (Vicar)
on Aug 26, 2011 at 14:53 UTC ( #922664=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://922664]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-05-23 17:59 GMT
Find Nodes?
    Voting Booth?