Bagaimana cara saya menggunakan regex menggunakan instr di VBA


-1

Saya mencoba menggunakan regex dan terus mendapatkan kesalahan (kesalahan kompiler) DAFTAR SEPARATOR. Adakah yang bisa memberitahu saya bagian mana yang salah.

 if instr(1,regex.Pattern([A-Z]?dtest(rt,1)),b)>0 then

Jawaban:


0

InStr () memiliki format ini :

InStr(startCharacter, searchInText, searchForText, compareMode)


startCharacter - a number (Long)
searchInText   - string (no RegEx, or pattern matching, or wildcard characters)
searchForText  - string (no RegEx, or pattern matching, or wildcard characters)
compareMode    - a number (from -1 to 2)

Ini mengembalikan angka (Varian - Panjang) - indeks di mana searchForText ditemukan di dalam searchInText


Coba gunakan opsi ini:

Option Explicit

Sub findTxt()
    Debug.Print InStrRegEx("987xyz", "[A-Z]")                   ' -> 4
    Debug.Print getText("987xyz", "[A-Z]")                      ' -> x

    Debug.Print InStr(1, "987xyz", "x")                         ' -> 4
    Debug.Print InStr(1, "987xyz", getText("987xyz", "[A-Z]"))  ' -> 4

    Debug.Print "987xyz" Like "*[A-Za-z]"                       ' -> True
End Sub

Public Function InStrRegEx(ByVal searchIn As String, ByVal searchFor As String) As Long
    Dim regEx As Object, found As Object
    If Len(searchIn) > 0 And Len(searchFor) > 0 Then
        Set regEx = CreateObject("VBScript.RegExp")
        regEx.Pattern = searchFor
        regEx.Global = True
        regEx.IgnoreCase = True
        Set found = regEx.Execute(searchIn)
        If found.Count <> 0 Then InStrRegEx = found(0).FirstIndex + 1
    End If
End Function

Public Function getText(ByVal searchIn As String, ByVal searchFor As String) As String
    Dim regEx As Object, found As Object
    If Len(searchIn) > 0 And Len(searchFor) > 0 Then
        Set regEx = CreateObject("VBScript.RegExp")
        regEx.Pattern = searchFor
        regEx.Global = True
        regEx.IgnoreCase = True
        Set found = regEx.Execute(searchIn)
        If found.Count <> 0 Then getText = CStr(found(0))
    End If
End Function
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.