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

Loo, Peter # PHX Peter.Loo at source.wolterskluwer.com
Wed Jun 28 10:05:11 PDT 2006


Hi David,

The problem is I won't know what position I need to change.  It is all
dependent on the query.

For example: Another query might return 10 ?s.  And within the 10 ?s, I
might need to change the 7th "?".

Does that make sense? 

Peter

-----Original Message-----
From: sanfrancisco-pm-bounces+peter.loo=source.wolterskluwer.com at pm.org
[mailto:sanfrancisco-pm-bounces+peter.loo=source.wolterskluwer.com at pm.or
g] On Behalf Of David Fetter
Sent: Wednesday, June 28, 2006 10:01 AM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] Quick question on regular expression and
substitution

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!
_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org
http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


This E-mail message is for the sole use of the intended recipient(s) and
may contain confidential and privileged information.  Any unauthorized
review, use, disclosure or distribution is prohibited.  If you are not
the intended recipient, please contact the sender by reply E-mail, and
destroy all copies of the original message.


More information about the SanFrancisco-pm mailing list