reg ex: how to exclude everything except numbers?!

Discussion in 'Programming' started by falcondriver, Aug 31, 2006.

  1. #1
    hi there,

    just spent 1 hour on some regex tutorials but couldnt find anything about this:
    how can i say "match everything EXCEPT [0-9]"?
    i have a mixed string with letters, numbers and some special chars and i only need all the numbers in it.
    sorry if this i a lame question, but all i found was "match this and this or maybe this only if its followed by that" but not such a basic thing...

    its for php preg_replace(), if this matters...
     
    falcondriver, Aug 31, 2006 IP
  2. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From what I remember it's

    [^0-9]
     
    void, Aug 31, 2006 IP
  3. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #3
    falcondriver, Sep 1, 2006 IP
  4. MattBeard

    MattBeard Peon

    Messages:
    259
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think we need a little more info.

    $data = 'teststring123.abc';
    preg_replace(/[0-9]/, '',$data);

    will make $data become 'teststring.abc'

    $data = 'teststring123.abc';
    preg_replace(/[^0-9]/, '',$data);

    will make $data become '123'
     
    MattBeard, Sep 1, 2006 IP
    falcondriver likes this.