OhBobSaget

StringRegExpReplace Help - Input string validation

18 posts in this topic

#1 ·  Posted (edited)

Hi,

I'm looking for some help regarding the use of StringRegExpReplace

To summarize what i am trying to do...

An input dialog that let you choose what pages you want to print.

Let's say i have a document of 50 pages and the user want to print only pages 1 to 5, page 10, pages 20 to 30 etc.

We would write in the input box : 1-5,10,20-30

 

So basically i need only numbers, comas and hyphen

Is there a way with StringRegExpReplace to check if anything else other than numbers,comas,hyphen are found ? 

 

 

Edited by OhBobSaget

Share this post


Link to post
Share on other sites



Rather check that the input has the expected format:

Local $aRanges = [ _
    "5", _
    "5,11", _
    "5,11-13", _
    "5,11-13,33", _
    "1-5,10,20-30", _
    "5-", _
    "5,11,", _
    "5,11-  13", _
    "5,*11-13,33", _
    "1-5,,10,20-30" _
]

For $sRange In $aRanges
    Local $valid = StringRegExp($sRange, "^(\d+(-\d+)?)(,(?1))*$")
    ConsoleWrite($sRange & " is " & ($valid ? "" : "in") & "valid" & @LF)
Next

Note that you may want to allow whitespaces and/or notations like "-6" to mean from start to 6, or "6-" meaning from 6 to end.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Rather check that the input has the expected format:

Local $aRanges = [ _
    "5", _
    "5,11", _
    "5,11-13", _
    "5,11-13,33", _
    "1-5,10,20-30", _
    "5-", _
    "5,11,", _
    "5,11-  13", _
    "5,*11-13,33", _
    "1-5,,10,20-30" _
]

For $sRange In $aRanges
    Local $valid = StringRegExp($sRange, "^(\d+(-\d+)?)(,(?1))*$")
    ConsoleWrite($sRange & " is " & ($valid ? "" : "in") & "valid" & @LF)
Next

Note that you may want to allow whitespaces and/or notations like "-6" to mean from start to 6, or "6-" meaning from 6 to end.

Not too sure if i can use this, because the ranges are not defined.  This is a InputBox and the user may add any number of pages he wants. 

 

he could virtually add any number of pages to extract.

ex :  1-3,5-8,10,14,16,19,25,26,30-34,100-120 etc.

So i am trying to check that the user will only use number 0 to 9 and - and , in the input box and then i will do more validation after this.

Share this post


Link to post
Share on other sites

I think i got something :

$forbidenCharacters = "[^(0-9)\^(,)\^(\-)]"

 

Share this post


Link to post
Share on other sites

BS,

This may help...ask if you have any questions...

; *** Start added by AutoIt3Wrapper ***
#include <WindowsConstants.au3>
; *** End added by AutoIt3Wrapper ***
; *** Start added by AutoIt3Wrapper ***
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
; *** End added by AutoIt3Wrapper ***
#include <GuiEdit.au3>
#include <array.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $gui010 = GUICreate('Print Pages', 250, 100)
Local $lbl010 = GUICtrlCreateLabel('Enter Page(s) to print: ', 10, 20, 110, 20, $ss_sunken)
Local $inp010 = GUICtrlCreateInput('', 140, 20, 100, 20)
Local $inp010_dummy = GUICtrlCreateDummy()
Local $btn010 = GUICtrlCreateButton('Submit', 10, 70, 230, 20)
GUISetState()

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $inp010_dummy
            $sText = _GUICtrlEdit_GetText($inp010)
            If $sText = '' Then ContinueLoop ; added in case the first char typed is invalid which caused loop due to $EN_CHANGE
            If Not StringRegExp($sText, "(?i)^[0-9-,]+\z") Then
                _GUICtrlEdit_SetText($inp010, StringTrimRight($sText, 1))
                _GUICtrlEdit_SetSel($inp010, StringLen($sText), StringLen($sText))
                _GUICtrlEdit_ShowBalloonTip($inp010, "Error", "You can only use characters 0-9 and '-' & ','", $TTI_ERROR)
            EndIf

            If StringInStr($sText, '--') Then
                _GUICtrlEdit_SetText($inp010, StringTrimRight($sText, 1))
                _GUICtrlEdit_SetSel($inp010, StringLen($sText), StringLen($sText))
                _GUICtrlEdit_ShowBalloonTip($inp010, "Error", "'-' can only be followed by a number", $TTI_ERROR)
            EndIf

            ;
            ; add edit for valid data on both sides of comma
            ;
    EndSwitch

WEnd



Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Switch BitShift($wParam, 16)
        Case $EN_CHANGE
            Switch BitAND($wParam, 0xFFFF)
                Case $inp010
                    GUICtrlSendToDummy($inp010_dummy)
            EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Bob,

Have you tried my sample script?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Bob,

Have you tried my sample script?

I haven't because i thought the range was static  - defined in this section :

Local $aRanges = [ _
    "5", _
    "5,11", _
    "5,11-13", _
    "5,11-13,33", _
    "1-5,10,20-30", _
    "5-", _
    "5,11,", _
    "5,11-  13", _
    "5,*11-13,33", _
    "1-5,,10,20-30" _
]

Is it dynamic ? i don't see how, since you define a specific $aRanges values.

If not, i might not fully understand your code.  I am still new to coding so...

Share this post


Link to post
Share on other sites

BS,

jchd's script works for validating all input at once and provides all the edits you need.  My script validates chars 1 by 1 as they are entered.  You need to decide which technique (or a combination) works for you.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

BS,

Ranges is just test data....it's the SRE that is important. 

Edited by kylomas
spelling
1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Bob,

Have you tried my sample script?

Tried it now, haven't realized it was test data.  Thank you kylomas for pointing that out lol.

The SRE works exactly as i want to.  Now i will try to understand why it does work with the documentation.

The parameters of StringRegExp are not that user friendly :)

 

Thank you both of you guys !

Share this post


Link to post
Share on other sites

Use http://regex101.com/ to test, debug and explain regexps.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

0 should be invalid

 

1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

0 should be invalid

 

I am validating if the value is 0 or 0-X in a statement after this.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

or 0,X

I think it can be handled by jchd's regexp with minimal adjustment.

Since I suck, I would add the additional check as well

Local $valid = StringRegExp($sRange , "0\z|0,.*|0-.*") ? FALSE : StringRegExp($sRange, "^(\d+(-\d+)?)(,(?1))*$" )

 

Edited by boththose
1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Of course: just replace every occurence of \d+ by 0*[1-9]\d*. I bet more validation belongs to the step where every subrange is validated (e.g. against allowable bounds). Now merging non-disjoint subranges to avoid duplication of output is yet another story: "1-3, 43,2-17, 38-51"

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

backwards is fine, it's still the same range (but as singles or range in all my efforts, it sorts and prints them in ascending order)

Edited by boththose
1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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