Jump to content

Problem with StringRegExp


Suppir
 Share

Recommended Posts

Hi!

Here is a code

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiEdit.au3>
$Form = GUICreate("", 400, 300, 300, 300)
$Input = GUICtrlCreateInput("Type regex here", 60, 0, 400, 20)
$Edit = GUICtrlCreateEdit("Some text", 0, 20, 400, 280, BitOR($ES_MULTILINE, $WS_VSCROLL))
$Button = GUICtrlCreateButton("Run", 2, 0, 55, 20, 0)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            Dim $Found
            GUICtrlSetState($Edit, $GUI_FOCUS)
            $Text=GUICtrlRead($Edit)
            $Regex=GUICtrlRead($Input)
            
            # HERE IS A PROBLEM
            $Found=StringRegExp($Text, $Regex, 1)
            _GUICtrlEdit_SetSel($Edit, 0, 1)
            
    EndSwitch
Wend

I want to search all matches(global search) in $Edit with regular expressions (in $Input). Than I want to set multiple selection with _GUICtrlEdit_SetSel() to all matches occured. I have two problems:

1) StringRegExp() returns matches(symbols), but I need offsets(numbers) to push it in _GUICtrlEdit_SetSel. How to get array of offsets of all matches?

2) _GUICtrlEdit_SetSel() sets the only one selection. If I tell to set another selection - the previous will be unselected. How to get multiple selection (in several places of $Edit) with this function?

Link to comment
Share on other sites

If you don't have to use StringRegExp's patternability you can do something like:

#include <Array.au3>

Local $avOffsets = _GetMatchesOffset("this is an isotropy property miss isolated", "is")
If Not @error Then _ArrayDisplay($avOffsets)

Func _GetMatchesOffset($sString, $sFind)
    Local $sOffsets = ""
    Local $iTmp = 0
    
    Do
        $iTmp = StringInStr($sString, $sFind, 0, 1, $iTmp+1)
        If $iTmp Then $sOffsets &= $iTmp & '|'
    Until Not $iTmp
    
    If Not StringLen($sString) Then SetError(1, 0, 0)
    Return SetError(0, 0, StringSplit(StringTrimRight($sOffsets, 1), '|'))
EndFunc

About highlighting the matches, you can use a rich edit control (search the Examples Forum). I don't think you can assume how much to highlight if you allow regular expressions because \d+ can match for 1 to any arbitrary string of digits length.

Link to comment
Share on other sites

Thanks! But I have to use StringRegExp...

Maybe you have seen PowerGREP application? All matches catched by regular expressions are highlighted yellow. I want to do the same (by selection or color).

Edited by Suppir
Link to comment
Share on other sites

#include <Array.au3>

Local $aMatches = _GetMatchesOffset("#1234ab cd567 efgh8", "\d+")
If IsArray($aMatches) Then _ArrayDisplay($aMatches)

Func _GetMatchesOffset($sString, $sFind)
    Local $re = ObjCreate("VBScript.RegExp")
    Local $oMatches

    $re.Global = 1
    $re.Pattern = $sFind
    $oMatches = $re.Execute($sString)

    If IsObj($oMatches) Then
        Local $avMatches[$oMatches.Count+1][3] = [[$oMatches.Count]]
        
        For $i = 0 To $oMatches.Count-1
            $avMatches[$i+1][0] = $oMatches($i).FirstIndex
            $avMatches[$i+1][1] = $oMatches($i).Length
            $avMatches[$i+1][2] = $oMatches($i).Value
        Next
        
        Return $avMatches
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc

Make sure the object is registered before you try to use it. It's by default registered in Windows operation systems, but check anyway.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...