Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Regex Question : Substitution

by azatoth (Curate)
on Feb 02, 2001 at 15:41 UTC ( #55977=perlquestion: print w/replies, xml ) Need Help??
azatoth has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys -

I have looked in Supersearch, but can't find anything close to my query...although, i couldn't really think of any good search strings either :P

#!/usr/bin/perl -w use strict; # variables my $eng; # main print "String : \n"; ($eng = <STDIN>); ($eng =~ s/u/a/i) if ($eng =~ /^.*u.*/g); print $eng;
The code above does the following. If I enter
"Jump up, Helmut!",
the result is
"Jamp up, Helmut!".
What I want though, is
"Jamp ap, Helmat!".
I.E Substitute every 'u' for an 'a', instead of just the first one.

Any ideas?


Azatoth a.k.a Captain Whiplash

Get YOUR PerlMonks Stagename here!

Replies are listed 'Best First'.
Re: Regex Question : Substitution
by BooK (Curate) on Feb 02, 2001 at 15:56 UTC

    Or even shorter, use the transliteration operator:

    $eng =~tr/u/a/;

    Documentation to be found at: tr and perlop

      Er, I think he wants t/Uu/Aa/


      oopsdate: Never mind. (I gotta keep this set at a larger font.)
Re: Regex Question : Substitution
by mirod (Canon) on Feb 02, 2001 at 15:54 UTC

    Just replace

    $eng =~ s/u/a/i) if ($eng =~ /^.*u.*/g);


    $eng =~ s/u/a/ig;

    There is no need to test whether the substitution will work, just do it.

    I think you wanted to write initially was more:

    $eng =~ s/u/a/i while ($eng =~ /^.*u.*/g);

    but even if this works it is really inneficient. To start with the /^.*u.*/ is strictly equivalent to /u/ except that it is slower (you should read Death to Dot Star! for more on the evils of .*)

Re: Regex Question : Substitution
by ChOas (Curate) on Feb 02, 2001 at 15:53 UTC

    use a 'g' in you substitution, like you do in your search...
    And if your searching every sting with a regex to see if you
    want to replace it, better just take the chance of replacing it
    Anyways... it won't hurt if there are no 'u's there...

    $eng =~ s/u/a/i => $eng =~ s/u/a/gio


    print "profeth still\n" if /bird|devil/;
Re: Regex Question : Substitution
by Beatnik (Parson) on Feb 02, 2001 at 18:27 UTC
    The funny part is, you're using g in m// while you only need to check once, and you're not using g in s/// when you want all occurances swapped.

    ... Quidquid perl dictum sit, altum viditur.
Re: Regex Question : Substitution
by dsb (Chaplain) on Feb 02, 2001 at 20:23 UTC
    $eng =~ s/u/a/gi;
    Using the 'g' and 'i' modifiers to: 1. Substitute every instance of 'u' 2. Ignore case - kel -

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2018-04-25 15:13 GMT
Find Nodes?
    Voting Booth?