[sf-perl] Quick question on regular expression and substitution

David Fetter david at fetter.org
Wed Jun 28 10:01:28 PDT 2006


On Wed, Jun 28, 2006 at 09:19:15AM -0700, Loo, Peter # PHX wrote:
> Hi All,
>  
> Would some one know of a one liner that would replace the third
> occurrence of a substring within a string?  For example:
>  
> $string = "?, ?, ?, ?";
>  
> I want to replace the third occurrence of "?" with "to_date(?,
> 'YYYY-MM-DD HH24:MI:SS')".
>  
> Thanks in advance.
>  
> Peter

Peter,

Given the high wackiness of regexes, I'd do something like:

$old = '(?, ?, ?, ?)'; # So you're covered on just exactly which clause.
$new = q{?, ?, to_date(?, 'YYYY-MM-DD HH24:MI:SS'), ?'};
$query =~ s/$old/$new/;

I know it's brute force, but the code is easy to understand later. :)

Cheers,
D
-- 
David Fetter <david at fetter.org> http://fetter.org/
phone: +1 415 235 3778        AIM: dfetter666
                              Skype: davidfetter

Remember to vote!


More information about the SanFrancisco-pm mailing list