match pattern "not"

Discussion in 'Perl Misc' started by Ela, Jun 26, 2008.

  1. Ela

    Ela Guest

    how to replace a line's all characters (case-insensitive) NOT equal to any
    of say, [ABCDE] to K?

    e.g.

    I am the sunny boy.

    to

    KKaKKKKeKKKKKKKbKKK
    Ela, Jun 26, 2008
    #1
    1. Advertising

  2. "Ela" <> wrote:
    >
    > how to replace a line's all characters (case-insensitive) NOT equal to any
    > of say, [ABCDE] to K?
    >
    > e.g.
    >
    > I am the sunny boy.
    > to
    > KKaKKKKeKKKKKKKbKKK


    Assuming you mean lower case [abcde], you can do it like this:

    $a = 'I am the sunny boy.';

    ($b = $a) =~ s/[^abcde]/K/g;

    print "$b\n";

    KKaKKKKeKKKKKKKbKKK
    Erwin van Koppen, Jun 26, 2008
    #2
    1. Advertising

  3. Erwin van Koppen <> wrote:

    > "Ela" <> wrote:
    > >
    > > how to replace a line's all characters (case-insensitive) NOT equal to any
    > > of say, [ABCDE] to K?
    > >
    > > e.g.
    > >
    > > I am the sunny boy.
    > > to
    > > KKaKKKKeKKKKKKKbKKK


    > Assuming you mean lower case [abcde], you can do it like this:


    > $a = 'I am the sunny boy.';


    > ($b = $a) =~ s/[^abcde]/K/g;


    > print "$b\n";


    And to make it case-insensitive just add 'i' to the match flags:

    ($b = $a) =~ s/[^ABCDE]/K/gi;

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jun 26, 2008
    #3
  4. Ela

    Mirco Wahab Guest

    Ela wrote:
    > how to replace a line's all characters (case-insensitive) NOT equal to any
    > of say, [ABCDE] to K?
    > I am the sunny boy.
    > to
    > KKaKKKKeKKKKKKKbKKK


    use the 'tr' operator:

    my $text = 'I am the sunny boy.';
    $text =~ tr/a-e/K/c;

    (http://perldoc.perl.org/perlop.html)

    Regards


    M.
    Mirco Wahab, Jun 26, 2008
    #4
  5. "Ela" <> wrote:
    >how to replace a line's all characters (case-insensitive) NOT equal to any
    >of say, [ABCDE] to K?


    perldoc perlop --> tr/// --> option c:
    c Complement the SEARCHLIST.

    >I am the sunny boy.
    >
    >to
    >
    >KKaKKKKeKKKKKKKbKKK


    But your text didn't contain any of [ABCDE], so every single character
    should be 'K', shouldn't it?

    jue
    Jürgen Exner, Jun 26, 2008
    #5
  6. On 2008-06-26 16:52, Jürgen Exner <> wrote:
    > "Ela" <> wrote:
    >>how to replace a line's all characters (case-insensitive) NOT equal to any

    ^^^^^^^^^^^^^^^^
    >>of say, [ABCDE] to K?

    [...]
    >>I am the sunny boy.
    >>
    >>to
    >>
    >>KKaKKKKeKKKKKKKbKKK

    >
    > But your text didn't contain any of [ABCDE],


    It did.

    hp
    Peter J. Holzer, Jun 29, 2008
    #6
  7. Ela

    Uri Guttman Guest

    >>>>> "SP" == Sherman Pendley <> writes:

    SP> Jürgen Exner <> writes:
    >> "Ela" <> wrote:
    >>> how to replace a line's all characters (case-insensitive) NOT equal to any
    >>> of say, [ABCDE] to K?

    >>
    >> perldoc perlop --> tr/// --> option c:
    >> c Complement the SEARCHLIST.
    >>
    >>> I am the sunny boy.
    >>>
    >>> to
    >>>
    >>> KKaKKKKeKKKKKKKbKKK

    >>
    >> But your text didn't contain any of [ABCDE], so every single character
    >> should be 'K', shouldn't it?


    SP> I think you may have missed the "case-insensitive" requirement
    SP> above. Which also means, incidentally, that the OP will need to use
    SP> s/// instead of tr///, given that the latter doesn't support the "i"
    SP> modifier.

    tr/// may not have /i but it can still do the job with:
    tr/abcdeABCDE/K/c

    now if the list of letters gets long it can become messier but that is
    up to the OP.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Jun 30, 2008
    #7
  8. "Peter J. Holzer" <> wrote:
    >On 2008-06-26 16:52, Jürgen Exner <> wrote:
    >> "Ela" <> wrote:
    >>>how to replace a line's all characters (case-insensitive) NOT equal to any

    >> But your text didn't contain any of [ABCDE],

    >
    >It did.


    Oooops, never mind.

    jue
    Jürgen Exner, Jun 30, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    642
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    368
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    479
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    163
Loading...

Share This Page