Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

match pattern "not"

0 views
Skip to first unread message

Ela

unread,
Jun 26, 2008, 9:20:14 AM6/26/08
to
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


Erwin van Koppen

unread,
Jun 26, 2008, 9:26:58 AM6/26/08
to

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

Jens Thoms Toerring

unread,
Jun 26, 2008, 9:45:43 AM6/26/08
to

> 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 ___ j...@toerring.de
\__________________________ http://toerring.de

Mirco Wahab

unread,
Jun 26, 2008, 9:47:08 AM6/26/08
to
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.

Jürgen Exner

unread,
Jun 26, 2008, 12:52:31 PM6/26/08
to
"Ela" <e...@yantai.org> 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

Peter J. Holzer

unread,
Jun 29, 2008, 4:57:06 PM6/29/08
to
On 2008-06-26 16:52, Jürgen Exner <jurg...@hotmail.com> wrote:
> "Ela" <e...@yantai.org> 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

Sherman Pendley

unread,
Jun 29, 2008, 5:20:38 PM6/29/08
to
Jürgen Exner <jurg...@hotmail.com> writes:

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

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net

Uri Guttman

unread,
Jun 29, 2008, 8:01:21 PM6/29/08
to
>>>>> "SP" == Sherman Pendley <spam...@dot-app.org> writes:

SP> Jürgen Exner <jurg...@hotmail.com> writes:
>> "Ela" <e...@yantai.org> 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 ------ u...@stemsystems.com -------- 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 ---------

Jürgen Exner

unread,
Jun 29, 2008, 8:05:46 PM6/29/08
to
"Peter J. Holzer" <hjp-u...@hjp.at> wrote:
>On 2008-06-26 16:52, Jürgen Exner <jurg...@hotmail.com> wrote:
>> "Ela" <e...@yantai.org> 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

0 new messages