VBA regular expression - matches an expression that does not start with a specific word

I want to create a VBA regular expression that finds the existence of two specific lines inside a set of parentheses.

For example, in this expression:

(aaa, bbb, ccc, ddd, xxx aaa)

he must somehow tell me what he found in the expression both “aaa” and “xxx aaa”. Ie, since there is a match on “aaa” without “xxxx” in front, and there is a match on “xxx aaa” in the expression, it should return true. Since these two sequences can be displayed in any order, the converse must also be true.

So, I think the expression / s would be something like this:

"(xxx aaa" [^ x] [^ x] [^ x] [^ x] aaa) "

to find words in the same order and

"(aaa" [^ x] [^ x] [^ x] [^ x] xxx aaa) "

for words in a different order.

It makes sense? Or is there a better approach?

I know this is a change in specification, but there is one important addition - there cannot be any intermediate parentheses between these words.

So, for example, this should not match:

(aaa, bbb, ccc, ddd, (eee, xxx aaa))

In other words, I am trying to find only a matching set of parentheses.

+5
source share
2 answers

Zero-width statements are your friend.

Function FindInParen(str As String, term1 As String, term2 As String) As Boolean
  Dim re As New VBScript_RegExp_55.RegExp

  re.Pattern = "\(" & _
               "(?=[^()]*)\)" & _
               "(?=[^()]*\b" & RegexEscape(term1) & "\b)" & _
               "(?=[^()]*\b" & RegexEscape(term2) & "\b)"

  FindInParen = re.Test(str)
End Function

Function RegexEscape(str As String) As String
  With New VBScript_RegExp_55.RegExp
    .Pattern = "[.+*?^$|\[\](){}\\]"
    .Global = True
    RegexEscape = .Replace(str, "\$&")
  End With
End Function

This template reads as:

  • Starting with the initial password, check:
    • that a suitable final paragraph follows somewhere and non-nested partners inside
    • what term1happens before the closing wig
    • what term2happens before the closing wig

look-ahead ((?=...)), , . , , term1 term2 , .

( " " ):

? FindInParen("(aaa, bbb, ccc, ddd, xxx aaa)", "aaa", "xxx aaa")
True

? FindInParen("(aaa, bbb, ccc, ddd, (eee, xxx aaa))", "aaa", "xxx aaa")
True

? FindInParen("(aaa, bbb, ccc, ddd, (eee, xxx aaa))", "bbb", "xxx aaa")
False

:

  • True, - - aaa xxx aaa .
  • Regex . . " " - /, . , .
  • "Microsoft VBScript Regular Expressions 5.5" .

FWIW, , :

Function FindInParen(str As String, term1 As String, term2 As String) As Boolean
  Dim parenPair As New VBScript_RegExp_55.RegExp
  Dim terms As New VBScript_RegExp_55.RegExp
  Dim matches As VBScript_RegExp_55.MatchCollection

  FindInParen = False
  parenPair.Pattern = "\([^()]*\)"
  terms.Pattern = "(?=.*?[(,]\s*(?=\b" & RegexEscape(Trim(term1)) & "\b))" & _
                  "(?=.*?[(,]\s*(?=\b" & RegexEscape(Trim(term2)) & "\b))"

  Do
    Set matches = parenPair.Execute(str)
    If matches.Count Then
      If terms.Test(matches(0).Value) Then
        Debug.Print "found here: " & matches(0).Value
        FindInParen = True
      End If
      str = parenPair.Replace(str, "[...]")
    End If
  Loop Until FindInParen Or matches.Count = 0

  If Not FindInParen Then
    Debug.Print "not found"
  End If

  If InStr("(", str) > 0 Or InStr(")", str) > 0 Then
    Debug.Print "mis-matched parens"
  End If
End Function

:

? FindInParen("(aaa, bbb, ccc, ddd, (eee, xxx aaa))", "aaa", "xxx aaa")
not found
False

? FindInParen("(aaa, bbb, ccc, ddd, (eee, xxx aaa))", "eee", "xxx aaa")
found here: (eee, xxx aaa)
True
+1

, (, , Regexp ), :

Sub Tester()
    RegexpTest ("(aaa, bbb, ccc, ddd, xxx aaa)")
End Sub


Sub RegexpTest(txt As String)
    Dim re As Object
    Dim allMatches, m

    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "([^,\(]*aaa)"
    re.ignorecase = True
    re.Global = True

    Set allMatches = re.Execute(txt)

    For Each m In allMatches
        Debug.Print Trim(m)
    Next m

End Sub
+1

All Articles