Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:06:25+00:00 2026-06-08T22:06:25+00:00

I’m looking to create a VBA regular expression that will find the existence of

  • 0

I’m looking to create a VBA regular expression that will find the existence of two particular strings inside a set of parentheses.

For example, in this expression:

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

it should somehow tell me that it found both “aaa” AND “xxx aaa” in the expression. I.e, since there is a match on “aaa” without the “xxxx ” in front, and there is also a match on “xxx aaa” later on in the expression, it should return true. Since these two sequences can appear in either order, the reverse should also be true.

So I’m thinking the expression/s would be something like this:

“(xxx aaa”[^x][^x][^x][^x]aaa)”

to find the words in one order and

“(aaa”[^x][^x][^x][^x]xxx aaa)”

for the words in another order.

Does this make sense? Or is there a better approach?

I know this is changing the spec, but there is one important addendum – there cannot be any interceding parentheses between the terms.

So for example, this should’t match:

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

In other words I’m trying to look in between a matching set of parentheses only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:06:29+00:00Added an answer on June 8, 2026 at 10:06 pm

    Zero-width look-ahead asserttions 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 pattern reads as:

    • Starting from an opening paren, check:
      • that a matching closing paren follows somewhere and no nested parens inside
      • that term1 occurs before the closing paren
      • that term2 occurs before the closing paren

    Since I’m using look-ahead ((?=...)), the regex engine never actually moves forward on the string, so I can chain as many look-ahead assertions and have them all checked. A side-effect is that the order in which term1 and term2 occur in the string doesn’t matter.

    I tested it on the console (“Immediate Window”):

    ? 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
    

    Notes:

    • The second test yields True because—technically—both aaa and xxx aaa are inside the same set of parens.
    • Regex cannot deal with nested structures. You will never get nested parentheses right with regular expressions. You will never be able to find “a matching set of parens” with regex alone – only an opening/closing pair that has no other parens in-between. Write a parser if you need to handle nesting.
    • Make a reference to “Microsoft VBScript Regular Expressions 5.5” in your project.

    FWIW, here’s a minimal nesting-aware function that works for the second test case above:

    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
    

    Console:

    ? 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.