Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: regex: how to negate a set of character ranges?

by Sidhekin (Priest)
on Apr 29, 2007 at 17:59 UTC ( [id://612658]=note: print w/replies, xml ) Need Help??


in reply to regex: how to negate a set of character ranges?

<clippy>It looks like you are trying to build a negated character class. Would you like some help?</clippy>

You have the syntax for including several ranges within a character class wrong. Simple rule: Within a character class, [, |, and whitespace (even with /x) are literal, and ] terminates the class (except in first position). So keep those square braces out until constructing the class, and keep the pipes and whitespace out, period.

Ah, but then, clippy may have been wrong? What you are trying to do is combine (and then negate) multi-character sequences, right?

Oh my. Combining is easy. What you have even works, though I'd personally use qr//x instead:

my $shiftjis = qr{ [\x30-\x39] | [\x41-\x59] | [\x61-\x7A] | [\x82-\x83][\x3F-\xFE] | [\x88][\x9E-\xFE] | [\x89-\xE9][\x3F-\xFE] | [\xEA][\x3F-\x9F] }x;

Negating is another subject alltogether, since there is more than one set of semantics for such a negation. I'm not entirely sure which makes more sense here ... if any! Here's one example/guess though:

while(<STDIN>){ chomp(); # Oops, nope -- variable-length lookbehind: # s/(?!$shiftjis).(?<!$shiftjis)//ogx; # This runs, but doesn't do the job: # s/(?!$shiftjis).//ogx; # This should work: s/(?!$shiftjis). (?<![\x30-\x39\x41-\x59\x61-\x7A]) (?<![\x82-\x83][\x3F-\xFE] |[\x88][\x9E-\xFE] |[\x89-\xE9][\x3F-\xFE] |[\xEA][\x3F-\x9F]) //ogx; print $_."\n"; }

But do you have to read STDIN as bytes? If you could read it as characters (in whatever encoding this is; see Encode), you'd be spared this mess.

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://612658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found