www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - regex: force entire string to match

reply NewName <b5658606 nwldx.com> writes:
Hello all.
I want to write a regex to check if a whole string is a number.
With my current regex("[0-9]+") numbers will be carved out of things like
"aaa456" (hit: 456) and I circumvent this by checking the lengths for
inequality, which is stupid. My regex is surely missing something?

Thank you for reading.
Jan 31 2012
next sibling parent Trass3r <un known.com> writes:
 I want to write a regex to check if a whole string is a number.
 With my current regex("[0-9]+") numbers will be carved out of things like
 "aaa456" (hit: 456) and I circumvent this by checking the lengths for
 inequality, which is stupid. My regex is surely missing something?

Try ^ and $ if applicable. These match start and end of a line.
Jan 31 2012
prev sibling next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Tue, 31 Jan 2012 16:29:43 +0000, NewName wrote:

 Hello all.
 I want to write a regex to check if a whole string is a number. With my
 current regex("[0-9]+") numbers will be carved out of things like
 "aaa456" (hit: 456) and I circumvent this by checking the lengths for
 inequality, which is stupid. My regex is surely missing something?
 
 Thank you for reading.

You want to match the beginning and end of the input as well: "^[0-9]+$"
Jan 31 2012
parent NewName <b5664031 nwldx.com> writes:
 You want to match the beginning and end of the input as well:
 "^[0-9]+$"

Thanks.
Jan 31 2012
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
NewName:

 Hello all.
 I want to write a regex to check if a whole string is a number.

Also: import std.stdio, std.string, std.array; void main() { string s = "123"; assert(s.removechars("0-9").empty); } Bye, bearophile
Jan 31 2012