Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

inline replacement in existing regex

by raiten (Acolyte)
on Nov 30, 2004 at 20:48 UTC ( #411277=perlquestion: print w/replies, xml ) Need Help??
raiten has asked for the wisdom of the Perl Monks concerning the following question:

Hi monkers

have a small problem which looks like
i want to make a replacement inline, so a replace in replace so here is the code

$content =~ s/(\w+)(: *)([^\n]*)\n<xxxxyyxxxx>\n/$1.":".$2.encode_base +64($3,'')."\n"/ges;

for now it doesn't work if $3 contains multiples lines (aka '\n')

first case, rewrite the rule to make replacement on multiple lines. In general i have few success with this.

seconde case, before this code, i decode the code. so there i can replace '\n' with '%%NEWLINE%%' for example and in the above line reverse.
s/%%NEWLINE%%/\n/ is ok, but how can i make it in the previous line ?

note: code use with perl/redhat enterprise or activeperl 5.8/winxp

Replies are listed 'Best First'.
Re: inline replacement in existing regex
by tall_man (Parson) on Nov 30, 2004 at 20:59 UTC
    Your question is very cryptic. Please include a bit more detail, such as some example input and desired output.
Re: inline replacement in existing regex
by duff (Vicar) on Nov 30, 2004 at 21:28 UTC

    It sounds like you're asking how to substitute newlines in the middle of that substitution. The reason you're asking is that

    it doesn't work if $3 contains multiples lines
    I think this premise is wrong. What do you mean that it "doesn't work"? I just tried a small test case (assuming that the encode_base64 in your code is the one from MIME::Base64) and it works just fine.

      if using
      $content =~ s/(\w+)(: *)(.*)\n<xxxxyyxxxx>\n/$1.":".$2.encode_base64($ +3,'')."\n"/ges;
      it works but it takes the biggest text segment, not the smallest one which i want.
      to answer to tall_man, i'm trying to clean the ldif output from mozilla to get standard ldap to include in a directory.
      so i get a file with multiple
      cn: dn=toto sn: XXX description: YYY

      some of them base64 encoded, i have to decode them, for cn, add a good suffix, remove others, reformat ones, ...
      some of theses base64 fields contains multiples lines and at the end of the conversion (reencoding base64) my file has:
      cn: dn=toto sn: XXX description: YYY yyyy <xxxxyyxxxx> cn: dn=toto2 sn: XXX2 description: YYY2 cn: dn=toto3 sn: XXX3 description: YYY3 tttt uuuuuu <xxxxyyxxxx>

      $content =~ s/(\w+)(: *)(.*)\n<xxxxyyxxxx>\n/$1.":".$2.encode_base64($ +3,'')."\n"/ges;

      => encode the largest matching
      $content =~ s/(\w+)(: *)([^\n]*)\n<xxxxyyxxxx>\n/$1.":".$2.encode_base +64($3,'')."\n"/ges;

      => doesn't match field with more than 1 lines. i hope have been clearer.


        It sounds like you just want to modify the greediness of *:

        $content =~ s/(\w+)(: *)(.*?)\n<xxxxyyxxxx>\n/$1.":".$2.encode_base64( +$3,'')."\n"/ges;
        The ? following the * will cause it to be non-greedy and thus match at the earliest possible point in the string rather than the latest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://411277]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-01-24 09:29 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.