Regex - every character but "

Discussion in 'Perl Misc' started by Cyde Weys, Mar 16, 2005.

  1. Cyde Weys

    Cyde Weys Guest

    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 :-/

    --
    ~ Cyde Weys ~
    So say we all.
     
    Cyde Weys, Mar 16, 2005
    #1
    1. Advertising

  2. Cyde Weys

    Ted Zlatanov Guest

    On Wed, 16 Mar 2005, wrote:

    > 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
     
    Ted Zlatanov, Mar 16, 2005
    #2
    1. Advertising

  3. Cyde Weys wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Mar 16, 2005
    #3
  4. Cyde Weys wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 16, 2005
    #4
  5. Cyde Weys

    Bob Walton Guest

    Bob Walton, Mar 17, 2005
    #5
  6. Bob Walton wrote:

    > Sherm Pendley wrote:
    >
    >> [\x00..\x7f]

    >
    > That should probably be:
    >
    > [\x00-\x7f]


    Yep. Got stuck in a for() loop. :-\

    Good catch, thanks.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Mar 17, 2005
    #6
    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. =?Utf-8?B?SnJNYw==?=

    Replace the 1st Character in every word

    =?Utf-8?B?SnJNYw==?=, Nov 18, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,448
    =?Utf-8?B?SmVyZW15IERhdmlz?=
    Nov 19, 2004
  2. danmc91
    Replies:
    3
    Views:
    574
    danmc91
    Oct 19, 2005
  3. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,466
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  4. Replies:
    3
    Views:
    781
    Reedick, Andrew
    Jul 1, 2008
  5. Sebastian
    Replies:
    17
    Views:
    357
    Gene Wirchenko
    Feb 4, 2013
Loading...

Share This Page