Regex: Matching Characters NOT in a Certain Range

H

Hal Vaughan

I can't find this in the FAQ, but I thought it was. If I have a range of
characters, like [a-zA-Z], how can I specify that I want to match any
character NOT in that range? (Yes, I know this is close to /W and /w, but
this is only an example).

I thought I remembered a characer I could use to invert a match like this so
if I were doing:

$line =~ s/[a-zA-Z]/x/g;

that if I used it, I could replace all characters NOT matching that range
instead of the ones that do.

Is there such a way to do this, or do I mis-remember?

Thanks!

Hal
 
A

Anno Siegel

Hal Vaughan said:
I can't find this in the FAQ, but I thought it was. If I have a range of
characters, like [a-zA-Z], how can I specify that I want to match any
character NOT in that range? (Yes, I know this is close to /W and /w, but
this is only an example).

I thought I remembered a characer I could use to invert a match like this so
if I were doing:

$line =~ s/[a-zA-Z]/x/g;

that if I used it, I could replace all characters NOT matching that range
instead of the ones that do.

Have you looked at perlre? You're looking for "^".

Anno
 
H

Hal Vaughan

Anno said:
Hal Vaughan said:
I can't find this in the FAQ, but I thought it was. If I have a range of
characters, like [a-zA-Z], how can I specify that I want to match any
character NOT in that range? (Yes, I know this is close to /W and /w,
but this is only an example).

I thought I remembered a characer I could use to invert a match like this
so if I were doing:

$line =~ s/[a-zA-Z]/x/g;

that if I used it, I could replace all characters NOT matching that range
instead of the ones that do.

Have you looked at perlre? You're looking for "^".

That's it!

I can never remember all the terms to search for. I tried regular
expression, regex and re. Forgot perlre.

I thought ^ was to match the beginning of a line, so my brain just wouldn't
accept it -- and I forgot to put it INSIDE the brackets!

Thanks!

Hal
 
A

Alan J. Flavell

That's it!

I can never remember all the terms to search for. I tried regular
expression, regex and re. Forgot perlre.

I thought ^ was to match the beginning of a line, so my brain just
wouldn't accept it -- and I forgot to put it INSIDE the brackets!

You have to keep a clear distinction between the mini syntax which
applies for character classes (inside [ ] ), and the more usual syntax
in regular expressions in general.
 
H

Hal Vaughan

A. Sinan Unur said:
perldoc perltoc

Thanks. I never knew that! (Seriously -- I was used to searching for
terms, but never new there was a toc!)

Hal
 
T

Tad McClellan

Hal Vaughan said:
Thanks. I never knew that! (Seriously -- I was used to searching for
terms, but never new there was a toc!)


Then you must have not even tried the somewhat obvious:

perldoc perl

which also lists the std docs, including the ToC.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top