Jump to content

RegExp question


Recommended Posts

Hi

 

I am struggeling around with an RegExp

 

I need to check a string, it should only contain digits and a -

 

This is what i have so far

 

StringRegExp($sColData,"[\s\D]",0)

which returns true for every non-digit (\D) character including @CR (which should not exist as well) and whitespaces (\s], but it returns true even if the string contains a - which should return 0

 

How can i fix that, i want like "match any non-digits and whitespace characters except for a -" ?

 

Best,

Thomy

Link to comment
Share on other sites

I need to check a string, it should only contain digits and a -

$sColData = "-123"
Msgbox(0,"", StringRegExp($sColData,"^[\d-]+$") )

If the - is for negative ints then :  StringRegExp($sColData,"^-?\d+$")

If the - is for separation between 2 numbers : StringRegExp($sColData,"^\d+-?\d*$")

You should be more precise concerning the requirements  :)

 

Edited by mikell
Link to comment
Share on other sites

Hi

 

The "-" should not exist in the string at all (it is used to separate the 2 numbers).

Further, CR LF should not exist in the string as well

 

So the string format will be <number1>-<number2> or only <number1>

 

Your pattern "^\d+-?\d*$" seems to detect CR/LF/CRLF everywhere in the string but not at the end (after the second number). If you could tell me a solution for this, it would be great :) (the number-only and "-" detection works fine)

Edited by Thomymaster
Link to comment
Share on other sites

Mikell's pattern "^\d+-?\d*$" does not match any CR/LF/CRLF. \d* is for \d 0 or more times.

But it could return true for 123- (with any number after the -)

Try this one :

StringRegExp($sColData,"^\d+(-\d+)?$")

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...