DxMxS Posted November 12, 2013 Share Posted November 12, 2013 Hello Everyone, I am kind of familiar with RegEx in sed scripts, however I am having a bit of a hard time writing a RegEx that I want to use with StringRegExpReplace(). I have text and codes in an edit control. What I want is to keep the codes (they all start with FWL) and get rid of all other text (including blank lines that may be found between codes before and after text is removed). For instance, a document that looks like: xxxxxxxxxxxxxxxxxxxxxxxxxxxx FWL9875632 yyyyyyyyyyyyyyyyyyyyyyyyyyyy FWL12345 zzzzzzzzzzzzzzzzzzzzzzzzzzzz [empty line] [empty line] FWL45678965 FWL985612565 bbbbbbbbbbbbbbbbbbbbbbbbb I want to process that text with StringRegExpReplace(), resulting in: FWL9875632 FWL12345 FWL45678965 FWL985612565 How can this be accomplished? I would appreciate a detailed explanation. Thanks in advance! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 DxMxS,Welcome to the AutoIt forum. I can get the relevant lines into an array using StringRegExp like this:#include <Array.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $aRet = StringRegExp($sText, "(?m)^(FWL\d*)$", 3) _ArrayDisplay($aRet)The pattern works like this:(?m) - Multiline: ^ and $ match at newline sequences within data ^ - So this means start at each new line and (FWL\d*) - capture anything beginning with FWL and the following numbers until $ - the end of each line 3 - Return the found groups as an arrayYou can easily transform the array into a file (_FileWriteFromArray) or a string (_ArrayToString). I hope that helps. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 Hi M23, Thanks for your reply. I will have to request further assistance from you, I tried to implement your approach in my code but I ended up with an empty edit This is the function I wrote: Func Filter() Local $eContent = ControlGetText ("Load List File Editor", "", $edit_Container) Local $arrayofCodes = StringRegExp($eContent, "(?m)^(FWLd*)$", 3) Local $newContent = _ArrayToString($arrayofCodes, "$arrayofCodes") ControlSetText("Load List File Editor" , "", $edit_Container, $newContent) EndFunc The purpose of this function is: Capture the content of the edit controller($edit_Container) in variable $eContent Send all matches of FWL codes to an array called $arrayofCodes Convert the array to a string, I think _ArrayToString($arrayofCodes, "$arrayofCodes") will use each item in the array as delimiter, hence print a list (?); capture the list in variable $newContent Print the content of variable $newContent to the edit ($edit_Container). Unfortunately, the function removed all content in the edit. What am I missing here? Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 DxMxS,Try using the correct syntax for _ArrayToString and you will find that it works: expandcollapse popup#include <Array.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $hGUI = GUICreate("Test", 500, 500) $cEdit = GUICtrlCreateEdit($sText, 10, 10, 480, 400) $cButton = GUICtrlCreateButton("Filter", 10, 450, 80, 30) GUICtrlSetState($cButton, $GUI_FOCUS) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton Filter() EndSwitch WEnd Func Filter() Local $eContent = ControlGetText($hGUI, "", $cEdit) Local $arrayofCodes = StringRegExp($eContent, "(?m)^(FWL\d*)$", 3) Local $newContent = _ArrayToString($arrayofCodes, @CRLF) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ControlSetText($hGUI, "", $cEdit, $newContent) EndFunc ;==>FilterAll good now? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 Thanks M23... however, I copy/pasted your code in a new file. When the app runs, I see the sample text in the edit, but it all goes away when I hit the "Filter" button. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 DxMxS,Add some errorchecking in the function to make sre you are getting sensible input and output:Func Filter() Local $eContent = ControlGetText($hGUI, "", $cEdit) MsgBox(0, "Input", $eContent) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Local $arrayofCodes = StringRegExp($eContent, "(?m)^(FWL\d*)$", 3) Local $newContent = _ArrayToString($arrayofCodes, @CRLF) MsgBox(0, "Output", $newContent) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ControlSetText($hGUI, "", $cEdit, $newContent) EndFunc ;==>FilterIf it looks as if it should be working and you are using SciTE to run this script, use ConsoleWrite in place of MsgBox and post the results that appear in the lower pane so that we can see what is going on. If that does not give us the solution we will look at saving the input and output to files. M23P.S. If you substitute a literal string in the ControlSetText command does that get pasted into the control? Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 M23, These are the results of my testing: The text auto populated by the app is shown in the input msgbox, however the output msgbox is empty. Other tests: 1) If I replace the auto populated as below, and the cursor remains next to the code: xxxxxxxxxxxxxxxxxxxxxxxxxxxx FWL9875632 The result is the desired one: The "xxxx..." is removed and the "FWL" code moves to first line in the edit. 1.2) If I replace the auto populated as below, and the cursor is positioned in the line below the FWL code: xxxxxxxxxxxxxxxxxxxxxxxxxxxx FWL9875632 The whole text is removed from the edit. 2) If I replace the auto populated text with 3 codes as below: FWL123 FWL456 FWL789 The first 2 FWL codes are removed and the 3rd one (FWL789) is moved to the first line in the edit *In this scenario if the cursor was in the empty line below the last FWL code, just as in test 1.2, the whole edit content was removed. Thanks for your patience and assistance. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 DxMxS, The text auto populated by the app is shown in the input msgbox, however the output msgbox is emptyThen there must be something funny about the text received from the edit. Let us save it so we can see what is in there:Func Filter() Local $eContent = ControlGetText($hGUI, "", $cEdit) FileWrite(@ScriptDir & "\input.txt", $eContent) ; <<<<<<<<<<<<<<<<<<<<<<<<<< Local $arrayofCodes = StringRegExp($eContent, "(?m)^(FWL\d*)$", 3) Local $newContent = _ArrayToString($arrayofCodes, @CRLF) ControlSetText($hGUI, "", $cEdit, $newContent) EndFunc ;==>FilterThat should create a file in the script folder. Please attach it rather than post it - you need to select the "Full Editor" option to see the necessary controls appear under the editor. I am at a loss to explain the other results - the cursor position should not have any effect on the read. What is this app from which you are reading? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 M23, I tested 3 times: First try was with the default text provided by your code. The other 2 tests, just as the ones I mentioned in the previous post, are manual inputs into the edit control. After each try I renamed the generated file so I could provide you with a file per test. input_FirstTry.txt input_SecondTry_CursorInEmptyLine.txt input_ThirdTry_CursorInSameLineThatLastCode.txt DxMxS Link to comment Share on other sites More sharing options...
mikell Posted November 12, 2013 Share Posted November 12, 2013 Melba My 2 cents Func Filter() GuiCtrlSetData($cEdit, StringRegExpReplace($sText, '(?s).*?((FWL\d*)|$)', '$2' & @crlf) ) EndFunc Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 (edited) Mikell, While waiting on comments from Melba I went ahead and tried your suggestion. The output is what we have been looking for. Thanks! Can you please explain your RegEx? Edited November 12, 2013 by DxMxS Link to comment Share on other sites More sharing options...
mikell Posted November 12, 2013 Share Posted November 12, 2013 Here is the whole #include <GUIConstantsEx.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $hGUI = GUICreate("Test", 500, 500) $cEdit = GUICtrlCreateEdit($sText, 10, 10, 480, 400) $cButton = GUICtrlCreateButton("Filter", 10, 450, 80, 30) GUICtrlSetState($cButton, $GUI_FOCUS) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton Filter() EndSwitch WEnd Func Filter() $ret = StringStripWS(StringRegExpReplace(GuiCtrlRead($cEdit), '(?s).*?((FWL\d*)|$)', '$2' & @crlf), 3) GuiCtrlSetData($cEdit, $ret) FileWrite(@desktopdir & "\my_test.txt", $ret) Msgbox(0,"", $ret &@crlf& "done") EndFunc Link to comment Share on other sites More sharing options...
DxMxS Posted November 12, 2013 Author Share Posted November 12, 2013 Mikell, Can you please explain your RegEx to me? Thanks, DxMxS Link to comment Share on other sites More sharing options...
mikell Posted November 12, 2013 Share Posted November 12, 2013 In everyday language the regex means : Find something + (FWL+some digits OR end-of-text), and replace this by the backreference #2, which is : (FWL+some digits OR end-of-text) (?s) : the dot matches anything including a newline Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 mikell,The result from your RegEx is the same as mine with 3 additional @CRLF at the end. I cannot understand why your result displays and mine does not - any ideas? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted November 12, 2013 Share Posted November 12, 2013 Melba, The more your regex is simple, the more it works (usually ...) #include <Array.au3> #include <GUIConstantsEx.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $hGUI = GUICreate("Test", 500, 500) $cEdit = GUICtrlCreateEdit($sText, 10, 10, 480, 400) $cButton = GUICtrlCreateButton("Filter", 10, 450, 80, 30) GUICtrlSetState($cButton, $GUI_FOCUS) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton Filter() EndSwitch WEnd Func Filter() Local $eContent = GuiCtrlRead($cEdit) Local $arrayofCodes = StringRegExp($eContent, 'FWL\d*', 3) Local $newContent = _ArrayToString($arrayofCodes, @CRLF) GuiCtrlSetData($cEdit, $newContent) EndFunc ;==>Filter Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 12, 2013 Moderators Share Posted November 12, 2013 mikell,The output from that is identical to the output from my original SRE. I understand how the result is abtained in every RegEx we have produced in this thread. What I do not understand is why your output displays - but as SREs are still, to misuse a quote from Winston Churchill. "a riddle, wrapped in a mystery, inside an enigma" I am not going to lose any sleep over it. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Solution DxMxS Posted November 12, 2013 Author Solution Share Posted November 12, 2013 Melba, Mikell - Thank you both for helping me with this. I am now marking this post as answered. DxMxS Link to comment Share on other sites More sharing options...
mikell Posted November 12, 2013 Share Posted November 12, 2013 Melba, I've always been very suspiscious dealing with newlines Local $arrayofCodes = StringRegExp($sText1, "(*CRLF)(?m)^(FWL\d*)$", 3) 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