Unix Programming - Identifying integer from a string

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2007 > Identifying integer from a string





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Identifying integer from a string
ruds

2007-04-28, 7:18 am

Hi,
I have a string such that it starts with a number and has words in
double qoutes appended to it like: 1234"ancb_adsds"
I want to know how can i extract only the number from the whole
string??
I not very good at regular exressions, but i tried comparing with
[0-9] in if block...but did'nt get any success.

Thanx in advance.

Jens Thoms Toerring

2007-04-28, 7:18 am

ruds <rudranee@gmail.com> wrote:
> I have a string such that it starts with a number and has words in
> double qoutes appended to it like: 1234"ancb_adsds"
> I want to know how can i extract only the number from the whole
> string??
> I not very good at regular exressions, but i tried comparing with
> [0-9] in if block...but did'nt get any success.


Sorry, but you must tell in which language you want to do that.
Since you mention regular expressions it's probably not for a
shell script since normal shells don't do regular expressions.
Should it be for a shell like bash anyway you could use e.g.

a='1234ancb_adsds"'
b=${a%%\"*}
echo $b

Or you could invoke sed (again with bash syntax):

a='1234ancb_adsds"'
b=`echo $a | sed 's/^\([0-9]*\).*$/\1/'`
echo $b

Mind the backticks around the echo and sed command, they are
required to catch the output of the command.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Pascal Bourguignon

2007-04-28, 7:18 am

ruds <rudranee@gmail.com> writes:
> I have a string such that it starts with a number and has words in
> double qoutes appended to it like: 1234"ancb_adsds"
> I want to know how can i extract only the number from the whole
> string??
> I not very good at regular exressions, but i tried comparing with
> [0-9] in if block...but did'nt get any success.


Since you don't tell which programming language, in my prefered one I
just write:

(parse-integer "1234\"ancb_adsds\"" :junk-allowed t)
--> 1234 ;
4 ; this is the last position parsed.


--
__Pascal Bourguignon__ http://www.informatimago.com/

"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com