Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.


Comment on Re: String replace, another question
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://922664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-07-25 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls