Jump to content
Sign in to follow this  
DxMxS

Assistance required to build RegEx

Recommended Posts

DxMxS

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!

 

Share this post


Link to post
Share on other sites
Melba23

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 array
You can easily transform the array into a file (_FileWriteFromArray) or a string (_ArrayToString). ;)

I hope that helps. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
DxMxS

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 :sweating:

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.

Share this post


Link to post
Share on other sites
Melba23

DxMxS,

Try using the correct syntax for _ArrayToString and you will find that it works: ;)

#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   ;==>Filter
All good now? :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
DxMxS

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

Share this post


Link to post
Share on other sites
Melba23

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   ;==>Filter
If 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. ;)

M23

P.S. If you substitute a literal string in the ControlSetText command does that get pasted into the control? :huh:


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
DxMxS

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.

Share this post


Link to post
Share on other sites
Melba23

DxMxS,

 

The text auto populated by the app is shown in the input msgbox, however the output msgbox is empty

Then 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   ;==>Filter
That 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. :wacko:

What is this app from which you are reading? :huh:

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
DxMxS

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

Share this post


Link to post
Share on other sites
DxMxS

Mikell,

While waiting on comments from Melba I went ahead and tried your suggestion. The output is what we have been looking for. Thanks! :ILA:

Can you please explain your RegEx?

Edited by DxMxS

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
DxMxS

Mikell,

Can you please explain your RegEx to me?

Thanks,

DxMxS

Share this post


Link to post
Share on other sites
mikell

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
 

Share this post


Link to post
Share on other sites
Melba23

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? :huh:

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
Melba23

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


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
DxMxS

Melba, Mikell -

Thank you both for helping me with this.

I am now marking this post as answered.

DxMxS

Share this post


Link to post
Share on other sites
mikell

Melba,

I've always been very suspiscious dealing with newlines

Local $arrayofCodes = StringRegExp($sText1, "(*CRLF)(?m)^(FWL\d*)$", 3)

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  

×