Regex: Matching Characters NOT in a Certain Range

Discussion in 'Perl Misc' started by Hal Vaughan, Oct 4, 2005.

  1. Hal Vaughan

    Hal Vaughan Guest

    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
    Hal Vaughan, Oct 4, 2005
    #1
    1. Advertising

  2. Hal Vaughan

    Anno Siegel Guest

    Hal Vaughan <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 4, 2005
    #2
    1. Advertising

  3. Hal Vaughan

    Hal Vaughan Guest

    Anno Siegel wrote:

    > Hal Vaughan <> wrote in comp.lang.perl.misc:
    >> 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
    Hal Vaughan, Oct 4, 2005
    #3
  4. Hal Vaughan <> wrote in
    news::

    > Anno Siegel wrote:


    ....

    >> Have you looked at perlre?


    ....

    > I can never remember all the terms to search for.


    perldoc perltoc

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 4, 2005
    #4
  5. On Tue, 4 Oct 2005, Hal Vaughan wrote:

    > Anno Siegel wrote:
    >
    > > 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!


    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.
    Alan J. Flavell, Oct 4, 2005
    #5
  6. Hal Vaughan

    Hal Vaughan Guest

    A. Sinan Unur wrote:

    > Hal Vaughan <> wrote in
    > news::
    >
    >> Anno Siegel wrote:

    >
    > ...
    >
    >>> Have you looked at perlre?

    >
    > ...
    >
    >> I can never remember all the terms to search for.

    >
    > perldoc perltoc


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

    Hal
    Hal Vaughan, Oct 4, 2005
    #6
  7. Hal Vaughan <> wrote:
    > A. Sinan Unur wrote:
    >
    >> Hal Vaughan <> wrote in
    >> news::
    >>
    >>> Anno Siegel wrote:

    >>
    >> ...
    >>
    >>>> Have you looked at perlre?

    >>
    >> ...
    >>
    >>> I can never remember all the terms to search for.

    >>
    >> perldoc perltoc

    >
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    930
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    457
    Ilias Lazaridis
    Sep 26, 2006
  3. Replies:
    3
    Views:
    731
    Reedick, Andrew
    Jul 1, 2008
  4. Clement Ow
    Replies:
    6
    Views:
    115
    Clement Ow
    Jun 25, 2008
  5. Frank Skare
    Replies:
    2
    Views:
    204
    Jim Cochrane
    Apr 17, 2004
Loading...

Share This Page