Jump to content

StringRegExp by patterns


Recommended Posts

Hi,
I'm trying to make a function that will catch all these phrases (in the array) that are case sensitive to wording that may contain other characters too 
the combination placements of (*) and (.) in the array are to be treated as patterns, so if the array should grow it will have the same patterns, only with different phrases

Starting out with this very raw example .. 
Main help is needed for the "StringRegExp" search patterns (broken in this example)
Of course ,Comments and suggestion on how this function may be improved are Welcome

 

Dim $wxpr[12]
$wxpr[1] = "word"
$wxpr[2] = "word.ext"
$wxpr[3] = "word.*"
$wxpr[4] = "*.ext"
$wxpr[5] = "*.word*"
$wxpr[6] = "*word*.*"
$wxpr[7] = "*.*word"
$wxpr[8] = "*word*.ext"
$wxpr[9] = "word*.*"
$wxpr[10] = "*word%word%word%word.*"
$wxpr[11] = "word%word%word*.*"

MsgBox(0, "_match", _match("this word and.ext"))

Func _match($aVal)
    For $i = 1 To UBound($wxpr) - 1
        If Not StringInStr($wxpr[$i], "*") Then
            ;should match to $wxpr[1] ,$wxpr[2]
            If $wxpr[$i] = $aVal Then
                Return 1
            EndIf
        ElseIf StringInStr($wxpr[$i], "*.*") Then
            Local $iPosition = StringInStr($wxpr[$i], "*.*")
            If $iPosition <> 1 Then
                StringReplace($wxpr[$i], "*", "*")
                If @extended > 2 Then
                    ;should match to $wxpr[6]
                    Local $a = StringReplace($wxpr[$i], "*", "")
                    $a = StringReplace($wxpr[$i], ".", "")
                    StringRegExp($aVal, "." & $a & "*\.*.", 0)
                    MsgBox(0, "StringRegExp", $wxpr[$i] & @CRLF & @error)
                    If @error = 1 Then Return 1
                Else
                    ;should match to $wxpr[11]
                EndIf
            Else
                ;should match to $wxpr[7]
            EndIf
        ElseIf StringInStr($wxpr[$i], ".*") Then
            StringReplace($wxpr[$i], "*", "*")
            If @extended > 1 Then
                ;should match to $wxpr[10]
            Else
                ;should match to $wxpr[3]
            EndIf
        ElseIf StringInStr($wxpr[$i], "*.") Then
            Local $iPosition = StringInStr($wxpr[$i], "*.")
            If $iPosition <> 1 Then
                ;should match to $wxpr[8]
            Else
                StringReplace($wxpr[$i], "*", "*")
                If @extended > 1 Then
                    ;should match to $wxpr[5]
                Else
                    ;should match to $wxpr[4]
                EndIf
            EndIf
        EndIf
    Next
    Return 0
EndFunc

 

Edited by Deye
Link to comment
Share on other sites

  • Moderators

Deye,

How about this:

#include <MsgBoxConstants.au3>

Global $wxpr[12] = [11, _
                   "word", _
                   "word.ext", _
                   "word.*", _
                   "*.ext", _
                   "*.word*", _
                   "*word*.*", _
                   "*.*word", _
                   "*word*.ext", _
                   "word*.*", _
                   "*word%word%word%word.*", _
                   "word%word%word*.*"]

_Match("this word and.ext")

Func _Match($sString)
    For $i = 1 To UBound($wxpr) - 1
        ; Convert to SRE pattern (escape PCRE chars and replace "*")
        $sPattern = StringReplace(StringRegExpReplace($wxpr[$i], "[][$^.{}()+\-]", "\\$0"), "*", ".*?")
        ; Check translation
        ConsoleWrite($wxpr[$i] & " - " & $sPattern & @CRLF)
        ; Try to match
        If StringRegExp($sString, $sPattern) Then
            MsgBox($MB_SYSTEMMODAL, "Match", $i & @CRLF & @CRLF & $wxpr[$i])
        EndIf
    Next

EndFunc

That seems to work for your test string.

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

 

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