Prev: FAQ 5.25 How do I get a file's timestamp in perl?
Next: FAQ 8.15 How do I set the time and date?
From: Willem on 25 Mar 2010 13:54 Ben Morrow wrote: ) It shouldn't need to be complicated. ) ) while ($content =~ /foo(\d+)/g) { ) my ($start, $end) = ($-[0], $+[0]); ) # this qq// should contain exactly what you would have put ) # in the RHS of the s/// ) my $after = qq/bar$1/; ) my $before = substr $content, $start, $end, $after; ) } I did something similar. However, because I didn't want to duplicate this code 5 times, I put it into a function, and that meant hand-parsing for $1, $2, etc. variables. Which made it quite complicated. However, the s///e solution mentioned crossthread had good potential: $content =~ s/<complicated expression (with parens)>/ func("<Another expression with $1 and stuff>")/ge; sub func { print "Sub '$&' by '$_[0]'"; $_[0]; } Which works like a charm. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT
From: Willem on 25 Mar 2010 13:55 sln(a)netherlands.com wrote: ) $content =~ s/(<add key=".*?\.foobar\.)(\d+)(" value="Foo=)(.*?)(;.*?"\/>)/ ) $tmp = $1.'10'.$3.'20'.$5; ) print "Substitution: '$1$2$3$4$5'\n => '$tmp'\n"; ) $tmp/eg; Yep, thanks, that worked quite well. (I put the expression into a function, and used $& instead of $1$2$3$4$5, though.) SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT
First
|
Prev
|
Pages: 1 2 Prev: FAQ 5.25 How do I get a file's timestamp in perl? Next: FAQ 8.15 How do I set the time and date? |