Regexp problem

Discussion in 'Perl Misc' started by kj, Feb 27, 2004.

  1. kj

    kj Guest

    I want to remove from a (variable) string any character whose ascii
    code is greater than 127, along with any whitespace preceding it.
    The only regexp I can think of to do this with is the abominable:

    /\s*(.)/ord($1) > 127 ? '' : $1/eg

    Is there a better way?

    Thanks!

    kj
    kj, Feb 27, 2004
    #1
    1. Advertising

  2. kj

    Ben Morrow Guest

    kj <> wrote:
    >
    > I want to remove from a (variable) string any character whose ascii
    > code is greater than 127, along with any whitespace preceding it.
    > The only regexp I can think of to do this with is the abominable:
    >
    > /\s*(.)/ord($1) > 127 ? '' : $1/eg
    >
    > Is there a better way?


    There *are* no characters whose ASCII codes are greater than 127, so
    this will do what you want:

    s/\s*[^[:ascii:]]//g;

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    Ben Morrow, Feb 27, 2004
    #2
    1. Advertising

  3. kj wrote:
    >
    > I want to remove from a (variable) string any character whose ascii
    > code is greater than 127,


    Anything greater than 127 is not ASCII.

    > along with any whitespace preceding it.
    > The only regexp I can think of to do this with is the abominable:
    >
    > /\s*(.)/ord($1) > 127 ? '' : $1/eg
    >
    > Is there a better way?


    s/\s*[^[:ascii:]]+//g;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Feb 27, 2004
    #3
  4. kj

    kj Guest

    In <c1n80f$11t$> kj <> writes:


    >I want to remove from a (variable) string any character whose ascii
    >code is greater than 127, along with any whitespace preceding it.
    >The only regexp I can think of to do this with is the abominable:


    > /\s*(.)/ord($1) > 127 ? '' : $1/eg


    Oops. Make that s/(\s*(\S))/ord($2) > 127 ? '' : $2/eg

    It's just as bad.

    >Is there a better way?


    >Thanks!


    >kj
    kj, Feb 27, 2004
    #4
  5. kj

    kj Guest

    Thanks!

    kj
    kj, Feb 27, 2004
    #5
    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. Greg Hurrell
    Replies:
    4
    Views:
    159
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    482
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    359
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    190
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    180
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page