Jump to content

StringRegExp - Verbatim sequence


Go to solution Solved by mikell,

Recommended Posts

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 by HurleyShanabarger
Link to comment
Share on other sites

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

 

 

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

  • Solution

@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

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 by MrCreatoR

 

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

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 by AspirinJunkie
Link to comment
Share on other sites

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

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_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

  • 1 year later...

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...