Jump to content
Sign in to follow this  
Thomymaster

RegExp question

Recommended Posts

Thomymaster

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
Thomymaster

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

Share this post


Link to post
Share on other sites
jguinch

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+)?$")

 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • mLipok
      By mLipok
      In April 5, 2013 I ask @Lazycat 
      he answer:
      Then I change this tool a little.
      Now I back to this and make bigger changed.
      Here is new version.
      Update History: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/07 v3.0 * Changed: AU3Check compilant - mLipok * Changed: almost all Variables renamed - mLipok * Added: "Delete RegExp Results" - mLipok * Added: support for dual monitor - mLipok * Added: "full screen mode" - mLipok  
    • MrCheese
      By MrCheese
      argh, pulling my hair out.
      considering this post: 
       
      say for a string = "03a", how can I strip out the leading 0 and the a.
      I have tried:
      $new = StringRegExpReplace($string, '[^1-9][^0-9]', '')
       
      and various combinations:
      ^0+[^0-9]
      [^[:digit:]]
      "[^0].*"
      "^0*(d+)"
       
      I'm going loopy!
       
       
    • PClough
      By PClough
      Hi everyone!
      After updating autoit, I tried to run an old program using complex regexp's.  It did not work.  Eventually I broke the problem down to this example:
       
      #include <Array.au3> $buf = "First title" & @CRLF & "Tom" & Chr(0x92) & "s sleepwalking" & @CRLF & "Last | line" & @CRLF $items = StringRegExp($buf, '([\x20-\xff]+)\x0d\x0a', 3) _ArrayDisplay($items,'') And this is the result I get when running it:
      Row 0
       
    • Miliardsto
      By Miliardsto
      Hello . How to do that
      $regexp = starts from "abcdef" and after this could be anything in name
      WinActivate($regexp)
×