Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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?

Thanks

Azatoth a.k.a Captain Whiplash

Get YOUR PerlMonks Stagename here!

Comment on Regex Question : Substitution
Download Code
Re: Regex Question : Substitution
by ChOas (Curate) on Feb 02, 2001 at 15:53 UTC
    Hi!

    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

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
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);

    by

    $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 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/

      p

      oopsdate: Never mind. (I gotta keep this set at a larger font.)
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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://55977]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-09-19 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls