Regex - every character but "

C

Cyde Weys

How do I write a negative character class? I.e. everything except a
certain character or a certain few characters? For instance, lets say I
wanted to represent all valid ASCII characters that can appear in a string.
It'd go something like this:

/"[A-Za-z0-9_ ~!@#\$%^&*()+\- ....... ]*"/ etc (some skipped for brevity)

Basically it would be everything on the keyboard except for "

There has to be a better way :-/
 
T

Ted Zlatanov

How do I write a negative character class? I.e. everything except a
certain character or a certain few characters? For instance, lets say I
wanted to represent all valid ASCII characters that can appear in a string.
It'd go something like this:

/"[A-Za-z0-9_ ~!@#\$%^&*()+\- ....... ]*"/ etc (some skipped for brevity)

Basically it would be everything on the keyboard except for "

There has to be a better way :-/

Read "perldoc perlre" carefully. It's very useful.

Negated character classes are done with [^abc], for instance, to mean
"everything except a, b, or c". So [^"] should do what you need.

Ted
 
S

Sherm Pendley

Cyde said:
How do I write a negative character class? I.e. everything except a
certain character or a certain few characters?

Prefix it with ^. As the first character in a regex, ^ anchors the match to
the beginning of the target, but as the first character in a character
class, it negates the class. So, the example class from the subject is:

[^"]
For instance, lets say I
wanted to represent all valid ASCII characters that can appear in a
string.

That's a different question entirely - the valid range of ASCII character
codes is 0-127, so a character class that includes all that is:

[\x00..\x7f]
Basically it would be everything on the keyboard except for "

"The" keyboard? No such beast. The characters available on any given kb will
vary.

Again, this contradicts both of your earlier questions. " is a perfectly
valid character that can appear in a string with no problem. And, there are
often *lots* of keys with codes that fall outside of the standard ASCII
range - especially when non-English users start typing accented characters
such as ü or ç.

sherm--
 
J

John W. Krahn

Cyde said:
How do I write a negative character class? I.e. everything except a
certain character or a certain few characters? For instance, lets say I
wanted to represent all valid ASCII characters that can appear in a string.
It'd go something like this:

/"[A-Za-z0-9_ ~!@#\$%^&*()+\- ....... ]*"/ etc (some skipped for brevity)

Basically it would be everything on the keyboard except for "

There has to be a better way :-/

/"[^"[:^ascii:]]*"/



John
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top