Sign in to follow this  
Followers 0
c.haslam

Validate RegExp

15 posts in this topic

StringRegExp() tests whether a string matches a particular pattern. But that is not what I am seeking.

I am writing a function that has a parameter that expects a RegExp -- any RegExp. I need code to check whether the RegExp is valid.

Does anyone know where I can find the code?


...chris

Share this post


Link to post
Share on other sites



Maybe >> http://regexlib.com/ could help?!


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

I use this function for testing RegEx patterns entered at run time.

Local $sPattern = "\Aabc(.*"
Local $ilocation = 0

If Not _IsRegExPatternValid($sPattern, $ilocation) Then
    MsgBox(0,"Invalid RegEx Pattern", "Error at character position " & $ilocation & " in pattern" & @CRLF & $sPattern)
EndIf

Func _IsRegExPatternValid($sPattern, ByRef $ilocation)
    Local $a = StringRegExp("",$sPattern,0)

    If @error = 2 Then
        $ilocation = @extended
        Return False
    EndIf

    Return True

EndFunc

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Maybe >> http://regexlib.com/ could help?!

I tried there. I searched for Autoit. No luck. Thanks for the suggestion.

...chris

Share this post


Link to post
Share on other sites

I use this function for testing RegEx patterns entered at run time.

I need to be able to set @error in my function when the calling function has an error.

...chris

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This might be what you are looking for

http://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression

It will have to be modified slightly because the PCRE as used by AutoIt does not recognize "/" as start and end of string.

Edit:

I don't have time to play with it right now but you may be able to do something like this

$sStr = "Some string which could even be the expression itself."
StringRegExp($sStr, < put your expression here>)
If @Error = 2 Then
    MsgBox(0, "Error", "There is an error in the expression at character " & @Extended)
EndIf

EDIT 2:

Now that I think about it, you should also be able to leave SsStr blank for the test you are trying to do.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

c.haslam,

are you expecting the function to execute the regex? if so, you will get return and @error info. if you are trying to validate the regex in a non-destructive way, then the problem is more than the regex being valid. you may need to mimic the target data as well.

@GEOSoft - thanks for that link...the headache should be gone by tommorrow :)

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

kylomas

You're most welcome. Take 2 aspirins and go to bed.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Func _Rexp_IsValid($sExp)
    StringRegExp("", $sExp)
    If @Error = 2 Then Return False
    Return True
EndFunc


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

There is also this one if you want to share the headache that kylomas has.

http://www.codeproject.com/KB/recipes/RegexTester.aspx


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Func _Rexp_IsValid($sExp)
    StringRegExp("", $sExp)
    If @Error = 2 Then Return False
    Return True
EndFunc

Thanks guys. The above code is exactly what I need.

  • I am writing a UDF which is based on a Corel PerfectScript library function. That function has a parameter that can be a predefined constant (e.g. Alphabetics!). or a string.
  • My first version had a Global available for this parameter (e.g. $_PS_ALPHA). But the rules of UDFs say "no globals".
  • I then thought of RegExps, e.g. "[:alpha:]".
  • Then there is another rule of UDFs: set @error for every parameter. That is where my question came from.
  • Then I thought: why not allow the guy calling the function to use any RegExp of his choice. So: how can a RegExp expression be validated? You have answered that question.
I am aware that no UDFs are being accepted now, but I needed more string functions for my own work. Writing them as though they are UDFs enforces a good discipline on me. I know it takes longer (I edited/wrote the GuiRIchEdit UDFs) but these functions will be code that I can trust.

BTW

$b = StringRegExp("9","[:digit:]")
MsgBox(0,"",$b&"  "&@error)
shows 0 0 but

$b = StringRegExp("9","[0-9]")
MsgBox(0,"",$b&"  "&@error)
shows 1 0. Is the error mine or in AutoIt? I took [:digit:] from the StringRegExp help.

...chris


...chris

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

StringRegExp("9","[[:digit:]]")

Edit:

Don't forget you can do:

StringRegExp("9","\d")

As well.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

BTW

$b = StringRegExp("9","[:digit:]")
MsgBox(0,"",$b&"  "&@error)
shows 0 0 but

$b = StringRegExp("9","[0-9]")
MsgBox(0,"",$b&"  "&@error)
shows 1 0. Is the error mine or in AutoIt? I took [:digit:] from the StringRegExp help.

...chris

Your Error in the first and no error in the second. A lot of people don't realize a charactert class must be placed within a class so you need to double up the [ and ]

$b = StringRegExp("9","[[:digit:]]")

also with that last bit of code I posted remember it doesn't need a string at all to test since we ar not testing for a match only for validity of the expression. The Toolkit in my signature uses primarily the same method to check but it also returns the offset of the error and sets the cursor to that point in the expression input which is more complex than you needed.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Many thanks. I don't use RegExp enough to remember the niceties.


...chris

Share this post


Link to post
Share on other sites

No problem. RegEx is a pain to learn and I still don't really like it but there are times when it is the best answer and it's worth the time it takes to get familiar with it.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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  
Followers 0