match pattern "not"

E

Ela

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
 
E

Erwin van Koppen

Ela said:
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
 
J

Jens Thoms Toerring

Erwin van Koppen said:
Ela said:
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
 
J

Jürgen Exner

Ela said:
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
 
P

Peter J. Holzer

Ela said:
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
 
U

Uri Guttman

SP> Jürgen Exner said:
Ela said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top