HurleyShanabarger Posted February 21, 2021 Share Posted February 21, 2021 (edited) If I want to prevent metacharacter having an effect on the regular expression, I can use \Q ... \E, where ... is the pattern. The search-pattern is entered by the user and the script actually adds some stuff before \Q and after \E. When the user is inserting \E in his pattern, it of course breaks the overall pattern. What would be the right approach to "escape", if the user enters \E? Dim $sText_1 = "C:\Test (example)\Everything" Dim $sUserPattern_1 = "Test (example)\Every" ConsoleWrite(StringRegExp($sText_1, "^.+?\Q" & $sUserPattern_1 & "\E.+?$") & @TAB & "This fails, as user inserts >>\E<<" & @CRLF) Dim $sText_2 = "C:\Test (example)\Fallout" Dim $sUserPattern_2 = "Test (example)\Fall" ConsoleWrite(StringRegExp($sText_2, "^.+?\Q" & $sUserPattern_2 & "\E.+?$") & @TAB & "No problem here" & @CRLF) Edited February 21, 2021 by HurleyShanabarger Link to comment Share on other sites More sharing options...
MrCreatoR Posted February 21, 2021 Share Posted February 21, 2021 The best solution is to escape specials chars: Dim $sText_1 = "C:\Test (example)\Everything" Dim $sUserPattern_1 = "Test (example)\Every" ConsoleWrite(StringRegExp($sText_1, "^.+?" & _StringRegExpEscapeChars($sUserPattern_1) & ".+?$") & @TAB & "No problem here" & @CRLF) Dim $sText_2 = "C:\Test (example)\Fallout" Dim $sUserPattern_2 = "Test (example)\Fall" ConsoleWrite(StringRegExp($sText_2, "^.+?" & _StringRegExpEscapeChars($sUserPattern_2) & ".+?$") & @TAB & "No problem here" & @CRLF) Func _StringRegExpEscapeChars($sString, $sChars = '') If StringStripWS($sChars, 8) = '' Then $sChars = '\]\[<>\^\\.+*?$(){}=!|:' EndIf Return StringRegExpReplace($sString, '([' & $sChars & '])', '\\\1') EndFunc HurleyShanabarger 1 Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Solution mikell Posted February 21, 2021 Solution Share Posted February 21, 2021 @MrCreatoR May I suggest to remove the $sChars param Dim $sText_1 = "C:\Test (example)\Everything" Dim $sUserPattern_1 = "Test (example)\Every" $p = _StringRegExpEscapeChars($sUserPattern_1, "\") ConsoleWrite($p & @CRLF) ConsoleWrite(StringRegExp($sText_1, "^.+?" & $p & ".+?$") & @CRLF) Func _StringRegExpEscapeChars($sString, $sChars = '') If StringStripWS($sChars, 8) = '' Then $sChars = '\]\[<>\^\\.+*?$(){}=!|:' EndIf Return StringRegExpReplace($sString, '([' & $sChars & '])', '\\\1') EndFunc Link to comment Share on other sites More sharing options...
MrCreatoR Posted February 21, 2021 Share Posted February 21, 2021 (edited) 3 hours ago, mikell said: May I suggest to remove the $sChars param Why? You are using it wrong, this param is to specify particular chars to escape... $p = _StringRegExpEscapeChars($sUserPattern_1, "\\\(\)") Edited February 21, 2021 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
AspirinJunkie Posted February 22, 2021 Share Posted February 22, 2021 (edited) If you want to keep your \Q...\E construct, you can also solve it like this: Dim $sText_1 = "C:\Test (example)\\\Everything" Dim $sUserPattern_1 = "Test (example)\\\Every" ConsoleWrite(StringRegExp($sText_1, "^.+?\Q" & StringRegExpReplace($sUserPattern_1, '\\E', '\\E\\\\E\\Q') & "\E.+?$") & @TAB & "This fails nothing more" & @CRLF) Dim $sText_2 = "C:\Test (example)\Eallout" Dim $sUserPattern_2 = "Test (example)\Eall" ConsoleWrite(StringRegExp($sText_2, "^.+?" & _RegExEscape($sUserPattern_2) & ".+?$") & @TAB & "No problem here" & @CRLF) ; same wrapped in a function Func _RegExEscape($sString) Return "\Q" & StringRegExpReplace($sString, '\\E', '\\E\\\\E\\Q') & "\E" EndFunc Also a escape function is a lot more easier with this approach. Edited February 22, 2021 by AspirinJunkie HurleyShanabarger 1 Link to comment Share on other sites More sharing options...
mikell Posted February 22, 2021 Share Posted February 22, 2021 9 hours ago, MrCreatoR said: Why? 1/ because it is not convenient. I know that I used it wrong but to be really handy such a param should be used like this... More, to make it work all the specials chars in the string must be first located by the user then mentioned and escaped in the param, so the function is not useful any more Dim $sText_1 = "C:\Test (example)\Everything" Dim $sUserPattern_1 = "Test \(example\)\\Every" ConsoleWrite(StringRegExp($sText_1, "^.+?" & $sUserPattern_1 & ".+?$") & @CRLF) 2/ because it is useless. The regex engine is powerful enough to find and treat all the specials chars by itself - and btw the user saves time Dim $sText_1 = "C:\Test (example)\Everything" Dim $sUserPattern_1 = "Test (example)\Every" $p = _StringRegExpEscapeChars($sUserPattern_1) ConsoleWrite($p & @CRLF) ConsoleWrite(StringRegExp($sText_1, "^.+?" & $p & ".+?$") & @CRLF) Func _StringRegExpEscapeChars($sString) Local $sChars = '\]\[<>\^\\.+*?$(){}=!|:' Return StringRegExpReplace($sString, '([' & $sChars & '])', '\\\1') EndFunc Link to comment Share on other sites More sharing options...
MrCreatoR Posted February 22, 2021 Share Posted February 22, 2021 47 minutes ago, mikell said: to make it work all the specials chars in the string must be first located by the user then mentioned and escaped in the param The idea is to allow to escape not all chars. 47 minutes ago, mikell said: it is useless I disagree (in this case yes, but sometimes no). Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
HurleyShanabarger Posted February 28, 2022 Author Share Posted February 28, 2022 I never cam back to you guys and just remembered as I run into the same issue. Thanks a lot, it solves my issue just perfectly! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now