Regex matching a integer in a line

D

dptom79

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
 
M

Martijn Lievaart

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
 
I

Iain Chalmers

Martijn Lievaart said:
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
 
J

Josef Moellers

Martijn said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top