Jump to content
Sign in to follow this  
Rodger

Regular expression with wildcards and extended ascii characters

Recommended Posts

Rodger

Hello,

I have the following code:

#include <StringConstants.au3>
#include <Array.au3>
 
$String = "öZaanïdam"
$SearchPattern = "?Zaan?dam"
 
; String
$String = StringStripWS($String,$STR_STRIPLEADING + $STR_STRIPTRAILING)
$String = StringReplace($String,".",".",0,1)
$String = StringReplace($String,"","",0,1)
 
;$SearchPattern
$SearchPattern = StringReplace($SearchPattern,"?",".",0,1)
 
If StringMid($SearchPattern,1,1) = "*" Then
$SearchPattern = StringReplace($SearchPattern,"*","<*.+",1,1)
EndIf
 
If StringMid($SearchPattern,StringLen($SearchPattern),1) = "*" Then
$SearchPattern = StringReplace($SearchPattern,"*",">*.+",-1,1)
EndIf
 
 
If StringRegExp($String,"b" & $SearchPattern & "b",0) Then ;Matched
$MatchedArray = StringRegExp($String,"b" & $SearchPattern  & "b",1)
_ArrayDisplay($MatchedArray)
EndIf
 
Exit
 
 
Can somebody explained to me why there isn't a match when using an extended ASCII character as the first character and how I can solve this ?
 
Below matches without a problem
 
$String = "xZaanïdam"
$SearchPattern = "?Zaan?dam"
 

 

Share this post


Link to post
Share on other sites
jchd

Please use AutoIt code tags to enclose code, it makes it easier to read.

The issue is with your use of the b assertion in default (non UCP) mode. b internally translates into a test whether "the current point is between characters not both w or W. See w for the meaning of "word".

Refer to the documentation for what w actually means to see the root cause of the behavior you observe.

If you want to see your last test to even match (contrary to the commentary found there), you must use the UCP mode with b, like this:

If StringRegExp($String,"(*UCP)\b" & $SearchPattern & "\b",0) Then ;Matched
    $MatchedArray = StringRegExp($String,"(*UCP)\b" & $SearchPattern  & "\b",1)
    _ArrayDisplay($MatchedArray)
EndIf

.


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
Rodger

Thank you jchd, it's working. :)  Sorry about the code, I will use the autoIt code tag next time.

Another small question:

How about things like this:

$String = "#Zaanïdam"

Or

$String = "-Zaanïdam"

So using some of the "reserved" characters of RE itself ?

#include <StringConstants.au3>
#include <Array.au3>

$String = "#Zaanïdam"
$SearchPattern = "?Zaan?dam"

; String
$String = StringStripWS($String,$STR_STRIPLEADING + $STR_STRIPTRAILING)
$String = StringReplace($String,".","\.",0,1)
$String = StringReplace($String,"\","\\",0,1)

;$SearchPattern
$SearchPattern = StringReplace($SearchPattern,"?",".",0,1)

If StringMid($SearchPattern,1,1) = "*" Then
    $SearchPattern = StringReplace($SearchPattern,"*","<*.+",1,1)
EndIf

If StringMid($SearchPattern,StringLen($SearchPattern),1) = "*" Then
    $SearchPattern = StringReplace($SearchPattern,"*",">*.+",-1,1)
EndIf

If StringRegExp($String,"(*UCP)\b" & $SearchPattern & "\b",0) Then ;Matched
    $MatchedArray = StringRegExp($String,"(*UCP)\b" & $SearchPattern  & "\b",1)
    _ArrayDisplay($MatchedArray)
EndIf

Exit
Edited by Rodger

Share this post


Link to post
Share on other sites
jchd

The pattern used in your regexp is now "(*UCP)b.Zaan.damb" after the initial transformation of your search pattern.

Applied to the subject string "#Zaanïdam" it doesn't match. To see that, remember the definition of b assertion: it means "non-word character followed by word character", word character being taken in the Unicode sense.

Because neither # (first subject) or - (second subject) are not word character and are at the beginning of both subjects, the actual condition is W (preceeding char is not present so it is "non-word") followed by non-word (# or -). So the first char in subject doesn't match b. The engine now tries at the next char in subject: Z is actually satisfying the b assertion. The engine proceeds now with the first dot in the pattern and matches it with the next char in subject, a. But next, the following char of the pattern Z doesn't match the second a in the subject. so the match fails.

In fact and because PCRE applies pattern optimizations, the engine returns a failure right after seeing that b doesn't match, as it knows that the pattern can't match because the length of the pattern is the same as the length of the subject, so failure is definitive after the first non-match which implies bumping the matching point in the subject.

You can see the effect of the sorter pattern "b." on those subject strings:

$MatchedArray = StringRegExp("#Zaanïdam", "(*UCP)\b.", 1)
_ArrayDisplay($MatchedArray)

I warmly recommend trying patterns with the excellent Online regex tester and debugger which features a regexp debugger showing you how the engine behaves step by step, should you need it. Don't forget to select the PCRE flavor to obtain results applicable to AutoIt.

Study the lengthy presentation in the StingRegExp() help and, if you need more complete specifications, dig into the official PCRE documentation (link below). I tried hard to make our help both concise and extensive but these are antagonistic goals and simply pasting the full PCRE pattern doc would repel beginners. Anyway, regexps are not always as sipmple as they may look, at least until you try them on real-world data!

Edited by jchd

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
guinness

Study the lengthy presentation in the StingRegExp() help

Come on, don't be modest, you wrote it. -_0

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_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: 22/04/2018

Share this post


Link to post
Share on other sites
jchd

That's why I said that being something between a quick-ref and the full doc, it forcibly leaves some aspects unclear. That's the price to pay for such tradeoff. It's nonetheless always enlightnening to observe what newcommers have issues understanding. I'm open to amending phrasing or adding some required details.


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

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  

×