Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

RE: RE: RE: Selective substitution: not in Perl?

by nuance (Hermit)
on Aug 16, 2000 at 14:49 UTC ( #28091=note: print w/replies, xml ) Need Help??


in reply to RE: RE: Selective substitution: not in Perl?
in thread Selective substitution: not in Perl?

The .* is greedy, as far as I can see that will always replace ithe last match if you have more than the number you need.
my string = "foo_a_foo_b_foo_c_foo_d_foo_e_foo_f_foo" my $pattern = "foo" my $better = "bar" my $n = 1; $string =~ s/^(($pattern.*){$n})$pattern/$1$better/;
would give:
"foo_a_foo_b_foo_c_foo_d_foo_e_foo_f_bar"
not
"foo_a_bar_b_foo_c_foo_d_foo_e_foo_f_foo"

Nuance

Replies are listed 'Best First'.
RE: RE: RE: RE: Selective substitution: not in Perl?
by davorg (Chancellor) on Aug 16, 2000 at 14:51 UTC

    You're absolutely right, of course. Now I remember why I didn't post that solution in the first place :(

    Update:

    On thinking about it further, I realise that:

    $string =~ s/^(($pattern.*?){$n})$pattern/$1$better/;

    will probably do the trick.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-01-19 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (55 votes). Check out past polls.

    Notices?