Regex matching a integer in a line

Discussion in 'Perl Misc' started by dptom79@gmail.com, Feb 21, 2007.

  1. Guest

    Hi experts,
    Can anyone tell me a regex to see if the line has a numeral only i.e,
    I dont want the integer in a line. I want the line which has only an
    integer while I am parsing a file..there will be many lines with
    integers but I want the line with ONLY integer in it (with some
    whitespaces which are to be avoided) and nothing else.
    Please help
    Thanks in advance
    Tom
    , Feb 21, 2007
    #1
    1. Advertising

  2. On Tue, 20 Feb 2007 22:54:40 -0800, dptom79 wrote:

    > Hi experts,
    > Can anyone tell me a regex to see if the line has a numeral only i.e,
    > I dont want the integer in a line. I want the line which has only an
    > integer while I am parsing a file..there will be many lines with
    > integers but I want the line with ONLY integer in it (with some
    > whitespaces which are to be avoided) and nothing else.


    /^[\s\d]+$/

    HTH,
    M4
    Martijn Lievaart, Feb 21, 2007
    #2
    1. Advertising

  3. In article <>,
    Martijn Lievaart <> wrote:

    > On Tue, 20 Feb 2007 22:54:40 -0800, dptom79 wrote:
    >
    > > Hi experts,
    > > Can anyone tell me a regex to see if the line has a numeral only i.e,
    > > I dont want the integer in a line. I want the line which has only an
    > > integer while I am parsing a file..there will be many lines with
    > > integers but I want the line with ONLY integer in it (with some
    > > whitespaces which are to be avoided) and nothing else.

    >
    > /^[\s\d]+$/


    Ummm... I don't think so:

    bigiain% perl -e'$_="1 2 3";print "OK\n" if /^[\s\d]+$/'
    OK

    how about

    /^\s*[\d]+\s*$/

    and

    /^\s*([\d]+)\s*$/

    if you want to capture the digits only.

    (extending to deal with negative integers left as an exercise for the
    reader)

    big

    --
    "Everything you love, everything meaningful with depth and history,
    all passionate authentic experiences will be appropriated, mishandled,
    watered down, cheapened, repackaged, marketed and sold to the people
    you hate." Mr Jalopy quoting Hooptyrides (on jalopyjunktown.com)
    Iain Chalmers, Feb 21, 2007
    #3
  4. Martijn Lievaart wrote:
    > On Tue, 20 Feb 2007 22:54:40 -0800, dptom79 wrote:
    >
    >
    >>Hi experts,
    >>Can anyone tell me a regex to see if the line has a numeral only i.e,
    >>I dont want the integer in a line. I want the line which has only an
    >>integer while I am parsing a file..there will be many lines with
    >>integers but I want the line with ONLY integer in it (with some
    >>whitespaces which are to be avoided) and nothing else.

    >
    >
    > /^[\s\d]+$/


    That will match " 1 2 3 ".

    If the OP wants single numbers only ("which has only an integer"), this
    /^\s*\d+\s*$/
    might be more appropriate.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Feb 21, 2007
    #4
    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. Ian Gil
    Replies:
    3
    Views:
    367
    Klaus-G. Meyer
    Oct 31, 2003
  2. Xah Lee
    Replies:
    1
    Views:
    922
    Ilias Lazaridis
    Sep 22, 2006
  3. Xah Lee
    Replies:
    8
    Views:
    451
    Ilias Lazaridis
    Sep 26, 2006
  4. Xah Lee
    Replies:
    2
    Views:
    205
    Xah Lee
    Sep 25, 2006
  5. Replies:
    2
    Views:
    378
Loading...

Share This Page