Professional Web Applications Themes

Replace with regular expression - PERL Beginners

Ricardo Pichler wrote: > Hi... > I need replace in my string the characters in > upperCase for the same but preceding the simbol +. > > Ex.: > $myName="RicardoPichler"; > s/.../.../g; # to do this return "+Ricardo+Pichler"; > > but like this: > $myName=~s/[A-Z]/\+[A-Z]/g; > but this donīt work. > You are attempting to use a character class as a replacement string which you don't want to do.... You would need something such as: my $string = 'RicardoPichler'; $string =~ s/([A-Z])/\+$1/g; This uses "capturing parentheses" to catch the character you want, in this case anything in the upper cased ...

  1. #1

    Default Re: Replace with regular expression

    Ricardo Pichler wrote:
    > Hi...
    > I need replace in my string the characters in
    > upperCase for the same but preceding the simbol +.
    >
    > Ex.:
    > $myName="RicardoPichler";
    > s/.../.../g; # to do this return "+Ricardo+Pichler";
    >
    > but like this:
    > $myName=~s/[A-Z]/\+[A-Z]/g;
    > but this donīt work.
    >
    You are attempting to use a character class as a replacement string
    which you don't want to do.... You would need something such as:

    my $string = 'RicardoPichler';
    $string =~ s/([A-Z])/\+$1/g;

    This uses "capturing parentheses" to catch the character you want, in
    this case anything in the upper cased character class and replace it
    with a plus sign followed by the result of the first set of capturing
    parenthesis.

    perldoc perlretut
    perldoc perlre
    perldoc perlvar

    [url]http://danconia.org[/url]

    Wiggins D'Anconia Guest

  2. #2

    Default Replace with regular expression

    Hi...
    I need replace in my string the characters in
    upperCase for the same but preceding the simbol +.

    Ex.:
    $myName="RicardoPichler";
    s/.../.../g; # to do this return "+Ricardo+Pichler";

    but like this:
    $myName=~s/[A-Z]/\+[A-Z]/g;
    but this donīt work.

    Can anyone help-me?

    Thanks in advance!
    Regards,
    Ricardo Pichler

    Ricardo Pichler Guest

  3. #3

    Default Re: Replace with regular expression

    On Nov 13, Ricardo Pichler said:
    >I need replace in my string the characters in
    >upperCase for the same but preceding the simbol +.
    >
    >$myName="RicardoPichler";
    >s/.../.../g; # to do this return "+Ricardo+Pichler";
    >
    >$myName=~s/[A-Z]/\+[A-Z]/g;
    You can't use a regex on the right-hand side of a s///, it's not
    appropriate. Instead, use capturing parentheses:

    $myName =~ s/([A-Z])/+$1/g;

    Another way to do this, though, is to use a look-ahead:

    $myName =~ s/(?=[A-Z])/+/g;

    That says "if we can look ahead and match an A-Z, then replace what we've
    matched (which isn't anything, we've only LOOKED) with a +, for all
    occurrences".

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  4. #4

    Default Re: Replace with regular expression

    Very thanks Jeff and Wiggins, this work perfectly!

    Regards,
    Ricardo Pichler
    ----- Original Message -----
    From: "Jeff 'japhy' Pinyan" <japhyperlmonk.org>
    To: "Ricardo Pichler" <ricardopichler.com.br>
    Cc: <beginnersperl.org>
    Sent: Friday, November 14, 2003 12:27 AM
    Subject: Re: Replace with regular expression

    > On Nov 13, Ricardo Pichler said:
    >
    > >I need replace in my string the characters in
    > >upperCase for the same but preceding the simbol +.
    > >
    > >$myName="RicardoPichler";
    > >s/.../.../g; # to do this return "+Ricardo+Pichler";
    > >
    > >$myName=~s/[A-Z]/\+[A-Z]/g;
    >
    > You can't use a regex on the right-hand side of a s///, it's not
    > appropriate. Instead, use capturing parentheses:
    >
    > $myName =~ s/([A-Z])/+$1/g;
    >
    > Another way to do this, though, is to use a look-ahead:
    >
    > $myName =~ s/(?=[A-Z])/+/g;
    >
    > That says "if we can look ahead and match an A-Z, then replace what we've
    > matched (which isn't anything, we've only LOOKED) with a +, for all
    > occurrences".
    >
    > --
    > Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    > RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    > <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    > [ I'm looking for programming work. If you like my work, let me know. ]
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    > For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    Ricardo Pichler Guest

Similar Threads

  1. help on regular expression
    By Madhu Reddy in forum PERL Beginners
    Replies: 5
    Last Post: January 28th, 01:31 AM
  2. Regular expression - replace non printable chars with spaces
    By Francesco in forum PHP Development
    Replies: 3
    Last Post: December 1st, 05:48 PM
  3. regular expression - help
    By kaptain kernel in forum PHP Development
    Replies: 4
    Last Post: November 10th, 08:04 PM
  4. regular expression help..
    By Matthew Vickers in forum PHP Development
    Replies: 15
    Last Post: September 16th, 10:54 AM
  5. how to replace urls in a doent (with regular expression)
    By Curious Expatriate in forum PHP Development
    Replies: 3
    Last Post: July 3rd, 04:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139