any STL/string function available for determing string contains numeric

N

nishit.gupta

Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
 
I

Ian Collins

Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
No. You would have to try one of the C library numeric conversion
functions on the string's contents.
 
N

nishit.gupta

No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me
 
J

Jacek Dziedzic

No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me

How about outputting the string to a stringstream and then
trying to read a double? If it reads sucessfully and the
stream does not have any characters left in it, it is a
number. I'm not sure if this will work for hex numbers
though, but you might try reading an integer as a side-check.

HTH,
- J.
 
I

Ian Collins

(e-mail address removed) wrote:

Please don't top-post.
No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me

Then you'll have to parse the string scanning for valid number patterns.
 
V

verdverm

Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero


you could try regexp's
http://freshmeat.net/projects/cpp_regex/

bool isValidNumber( string str ) {

if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;

if( num is valid )
return true
else
return false
}

} // end of isValidNumber

you could even return an int or enumeration to determine what type
with further tests
 
J

James Kanze

Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero

Boost::regex.
 
J

Jacek Dziedzic

verdverm said:
you could try regexp's
http://freshmeat.net/projects/cpp_regex/

bool isValidNumber( string str ) {

if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;

if( num is valid )
return true
else
return false

Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.

Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
}

} // end of isValidNumber

you could even return an int or enumeration to determine what type
with further tests

- J.
 
J

James Kanze

verdverm said:
you could try regexp's
http://freshmeat.net/projects/cpp_regex/
bool isValidNumber( string str ) {

if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;
if( num is valid )
return true
else
return false
Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.

The usual formulation in such cases is:

return oss >> num >> std::ws && oss.get() == EOF ;

If you don't want to allow whitespace, do
oss.unsetf( std::ios::skipws ) ;
before, and drop the "<< std::ws".

The problem here is that there is no one target type which will
accept all possible numbers. If you're willing to use two
streams, however, something like the following should work:

std::eek:stringstream si( str ) ;
si.unsetf( std::ios::basefield ) ;
std::eek:stringstream sd( str ) ;
int i ;
double d ;
return (si >> i >> std::ws && si.get() == EOF)
|| (sd >> d >> std::ws && sd.get() == EOF) ;

Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.

The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)

With boost::regex, you can capture the substrings which matched
parts of the regular expression in parentheses. Within the
or's, only one should not be empty, and which one tells you
which or matched.

My own regular expression class allows matching several regular
expressions in parallel, with the return code indicating which
one matched:


static Gabi::RegularExpression const
number =
Gabi::RegularExpression( "[+-]?[1-9][0-9]*", 10 ) ;
| Gabi::RegularExpression( "[+-]?0[0-7]*", 8 ) ;
| Gabi::RegularExpression( "[+-]?0[Xx][0-9a-fA-F]+", 16 )
| Gabi::RegularExpression( "[+-]?[0-9]+\.[0-9]*([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?\.[0-9]+([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?[0-9]+[Ee][+-]?[0-9]+",
0 ) ;

std::pair< int, std::string::const_iterator >
result
= number.match( str.begin(), str.end() ) ;
return result.second == str.end()
? result.first
: -1 ;

This would return -1 if no match, 0 for a floating point value,
and the base for an integer value. (I've since added features
to force an error if the match doesn't go to the end of the
string, so you could just use "return number.match(...).first;",
but this version isn't yet present at my site.)
 
N

nishit.gupta

Thanx James...1 more query..is regex a part of standard C++???
James said:
verdverm said:
On Apr 4, 12:22 am, (e-mail address removed) wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
you could try regexp's
http://freshmeat.net/projects/cpp_regex/
bool isValidNumber( string str ) {

if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;
if( num is valid )
return true
else
return false
Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.

The usual formulation in such cases is:

return oss >> num >> std::ws && oss.get() == EOF ;

If you don't want to allow whitespace, do
oss.unsetf( std::ios::skipws ) ;
before, and drop the "<< std::ws".

The problem here is that there is no one target type which will
accept all possible numbers. If you're willing to use two
streams, however, something like the following should work:

std::eek:stringstream si( str ) ;
si.unsetf( std::ios::basefield ) ;
std::eek:stringstream sd( str ) ;
int i ;
double d ;
return (si >> i >> std::ws && si.get() == EOF)
|| (sd >> d >> std::ws && sd.get() == EOF) ;

Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.

The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)

With boost::regex, you can capture the substrings which matched
parts of the regular expression in parentheses. Within the
or's, only one should not be empty, and which one tells you
which or matched.

My own regular expression class allows matching several regular
expressions in parallel, with the return code indicating which
one matched:


static Gabi::RegularExpression const
number =
Gabi::RegularExpression( "[+-]?[1-9][0-9]*", 10 ) ;
| Gabi::RegularExpression( "[+-]?0[0-7]*", 8 ) ;
| Gabi::RegularExpression( "[+-]?0[Xx][0-9a-fA-F]+", 16 )
| Gabi::RegularExpression( "[+-]?[0-9]+\.[0-9]*([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?\.[0-9]+([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?[0-9]+[Ee][+-]?[0-9]+",
0 ) ;

std::pair< int, std::string::const_iterator >
result
= number.match( str.begin(), str.end() ) ;
return result.second == str.end()
? result.first
: -1 ;

This would return -1 if no match, 0 for a floating point value,
and the base for an integer value. (I've since added features
to force an error if the match doesn't go to the end of the
string, so you could just use "return number.match(...).first;",
but this version isn't yet present at my site.)

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
J

Jacek Dziedzic

James said:
Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;

That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?
The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)

Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().

cheers,
- J.
 
J

James Kanze

James Kanze wrote:
Personally, I'd go with something like:
static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?

Not in C++. On the other hand, you can have suffixes which
specify the type more precisely: U, L or LL for integers, and F
or L for floating point. (Lower case is also accepted, and U
can combine with L or LL.) Thus, 3.14: double, 3.14F: float and
3.14L: long double. I don't know whether the original poster
wanted to accept these or not.

Note that it might be clearer if you specified some of the parts
as constant strings, and build the strings up, rather than
repeating things like "[Ee][+-]?[0-9]+" umpteam times. And
also, that my knowledge of regular expressions was acquired
many, many years ago, and hasn't been updated; I think that
Boost (and some others) accept things like \d for [0-9], \w for
[:space:], etc. (But don't forget that for regex to see the \
in a string literal, you have to double it.) Before using
something like this, I would strongly recommend reading the
documentation for the regular expression package you use.
Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().

Only at the start of conversion. Otherwise, think of the
problems: reading "123" into an int would fail.

The conditions in ios (the base class of istream and ostream)
aren't all that clear: the function good(), for example, does
consider eof(), so something like "if ( stream.good() )" after
an attempted read can fail, even though the read succeeded.
This is one of the reasons why one normally just uses the
conversion to bool (actualy, void*): it tests what you have to
test to know whether the conversion succeeded, and it doesn't
test anything else.

On top of that, the rules for unformatted input are somewhat
different, and the fact that the read "fails" doesn't mean that
it really failed. It only really failed if "! stream &&
stream.gcount() == 0".
 
J

James Kanze

On Apr 5, 12:08 pm, (e-mail address removed) wrote:

[I thought I'd answered this, but since it's not showing
up...]
Thanx James...1 more query..is regex a part of standard C++???

Sort of. It's part of the TR1 (and so already available in some
libraries, without separately installing Boost), and it's been
adopted for C++0x, so it will be fully part of the standard
library in a couple of years.

In the meantime, if your compiler will support it, it's
definitly worth the bother of installing Boost (and installing
Boost is a pain, because they use a very non-standard and very
broken tool for building).

--
 
J

Jacek Dziedzic

James said:
James Kanze wrote:
Personally, I'd go with something like:
static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?

Not in C++.

I must have gotten contaminated by Fortran, even though
I steer away from it :).
On the other hand, you can have suffixes which
specify the type more precisely: U, L or LL for integers, and F
or L for floating point. (Lower case is also accepted, and U
can combine with L or LL.) Thus, 3.14: double, 3.14F: float and
3.14L: long double. I don't know whether the original poster
wanted to accept these or not.

Right.
Note that it might be clearer if you specified some of the parts
as constant strings, and build the strings up, rather than
repeating things like "[Ee][+-]?[0-9]+" umpteam times. And
also, that my knowledge of regular expressions was acquired
many, many years ago, and hasn't been updated; I think that
Boost (and some others) accept things like \d for [0-9], \w for
[:space:], etc. (But don't forget that for regex to see the \
in a string literal, you have to double it.) Before using
something like this, I would strongly recommend reading the
documentation for the regular expression package you use.

OK
Only at the start of conversion. Otherwise, think of the
problems: reading "123" into an int would fail.

The conditions in ios (the base class of istream and ostream)
aren't all that clear: the function good(), for example, does
consider eof(), so something like "if ( stream.good() )" after
an attempted read can fail, even though the read succeeded.
This is one of the reasons why one normally just uses the
conversion to bool (actualy, void*): it tests what you have to
test to know whether the conversion succeeded, and it doesn't
test anything else.

Right.
On top of that, the rules for unformatted input are somewhat
different, and the fact that the read "fails" doesn't mean that
it really failed. It only really failed if "! stream &&
stream.gcount() == 0".

Yes.

thanks,
- J.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top