Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > match pattern "not"

Reply
Thread Tools

match pattern "not"

 
 
Ela
Guest
Posts: n/a
 
      06-26-2008
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


 
Reply With Quote
 
 
 
 
Erwin van Koppen
Guest
Posts: n/a
 
      06-26-2008

"Ela" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      06-26-2008
Erwin van Koppen <(E-Mail Removed)> wrote:

> "Ela" <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      06-26-2008
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.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-26-2008
"Ela" <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      06-29-2008
On 2008-06-26 16:52, Jürgen Exner <(E-Mail Removed)> wrote:
> "Ela" <(E-Mail Removed)> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-30-2008
>>>>> "SP" == Sherman Pendley <(E-Mail Removed)> writes:

SP> Jürgen Exner <(E-Mail Removed)> writes:
>> "Ela" <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-30-2008
"Peter J. Holzer" <(E-Mail Removed)> wrote:
>On 2008-06-26 16:52, Jürgen Exner <(E-Mail Removed)> wrote:
>> "Ela" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments