Jump to content

[Solved] StringRegExpReplace() Question


Recommended Posts

HiHo Community,

I want to create an Input Control which allows only positive 5 digit numbers with max. 2 digit decimal as input. $ES_NUMBER does not allow digits, so i thought I'll define a wm_notify or loop using StringRegExpReplace(), but I could not figure out the correct pattern. Maybe someone with deeper RegEx knowledge could help me out?

Valid:

99999.99 (only 1 dot allowed)

1.1

1

0.1 (forcing a 0 in front if . is entered first possible? Possible for sure :), whats the pattern?)

Invalid:

Non-Numeric and not dot

-1

using something like

guictrlsetdata($input,StringRegExpReplace(guictrlread($input),"???","$1.$2"))

Best Regards

Edit: The "forcing 0 in front" part is in fact quiet easy, after StringRegExpReplace() I'll just append an

if stringleft(guictrlread($input,1),1) = "." then guictrlsetdata($input,0 & guictrlread($input)) :party:, so skip that.

Edit 2: Appended (not working correct yet) test code.

So literally the expression would be something like (1-5 digits AND/OR 1 dot AND 0-2 digits).

#include<GUIConstantsEx.au3>

GUICreate("Test")
$input = GUICtrlCreateInput("", 10, 10, 100)
$input_save = ""
GUISetState()

Do
    Sleep(10)
    $input_read = GUICtrlRead($input)
    if $input_save <> $input_read Then
        $input_read = StringRegExpReplace($input_read,"[^\d]","$1")
        if stringleft($input_read,1) = "." then $input_read = 0 & $input_read
        GUICtrlSetData($input, $input_read)
        $input_save = $input_read
    endif
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Edited by KaFu
Link to comment
Share on other sites

  • Moderators

$input_read = StringRegExpReplace($input_read,"^(\D*)(\d{0,5})(\d+)?(\.)?(\d{0,2})?(.*?)\z","\2\4\5")

Edit:

Interesting enough, I had to change that up a bit:

$input_read = StringRegExpReplace($input_read,"^(\D*)(\d(?:\d?\d?\d?\d?))(\d+)?(\.)?(\d{0,2})?(.*?)\z","\2\4\5")
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Check the UDF RestrictControlRegExp, and use it as following:

#include <GUIConstantsEx.au3>
#include <RestrictControlRegExp.au3>
;

_RegEx_RestrictControl_Setup()

GUICreate("Test")

$input = GUICtrlCreateInput("", 10, 10, 100)
_RegEx_RestrictControl_Add($input, "^[0-9]{1,5}\.[0-9]{0,2}$", "99999.99")

$input_save = ""

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

It's almost what you need, except the behaviour on entered dot at the begining.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Interesting enough, I had to change that up a bit

Perfect :), seems to fit my needs. Thanks a lot!

#include<GUIConstantsEx.au3>

GUICreate("Test")
$input = GUICtrlCreateInput("", 10, 10, 100)
$input_save = ""
GUISetState()

Do
    $input_read = GUICtrlRead($input)
    if $input_save <> $input_read Then
        if stringleft($input_read,1) = "." then $input_read = 0 & $input_read
        $input_read = StringRegExpReplace($input_read,"^(\D*)(\d(?:\d?\d?\d?\d?))(\d+)?(\.)?(\d{0,2})?(.*?)\z","\2\4\5")
        GUICtrlSetData($input, $input_read)
        $input_save = $input_read
    endif
Until GUIGetMsg() = $GUI_EVENT_CLOSE

in a wm_notify if the input looses focus.

Link to comment
Share on other sites

It's almost what you need, except the behaviour on entered dot at the begining.

What should be solved with an

if stringleft($input_read,1) = "." then $input_read = 0 & $input_read

somewhere within that function too, will definitely check it out. Thanks to you too :)...

Link to comment
Share on other sites

$input_read = StringRegExpReplace($input_read,"^(\D*)(\d{0,5})(\d+)?(\.)?(\d{0,2})?(.*?)\z","\2\4\5")

Edit:

Interesting enough, I had to change that up a bit:

$input_read = StringRegExpReplace($input_read,"^(\D*)(\d(?:\d?\d?\d?\d?))(\d+)?(\.)?(\d{0,2})?(.*?)\z","\2\4\5")
You keeping amaze me with those RegExp patterns every time, you are the best in it! :)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...