Identifying integer from a string
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Identifying integer from a string




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Identifying integer from a string  
ruds


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-28-07 12:18 PM

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.






[ Post a follow-up to this message ]



    Re: Identifying integer from a string  
Jens Thoms Toerring


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-28-07 12:18 PM

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





[ Post a follow-up to this message ]



    Re: Identifying integer from a string  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-28-07 12:18 PM

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."





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:33 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register