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

  • Home
  • SEARCH
  • 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 3677296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:12:15+00:00 2026-05-19T03:12:15+00:00

I’m asking this partially because it is something I would find very useful, but

  • 0

I’m asking this partially because it is something I would find very useful, but also to understand the syntax of the answer so I can make my own, creative, better regular expressions!

So here’s the question: I want to find a function in my code and I know part of the function name, and something that is inside of the function. For example, given these functions:

Foo::bar1(Baz* baz)
{
    ...
    doWork();
    ...
}

Foo::bar2()
{
    ...
    beHappy();
    ...
}

Foo::hasAbar3Meat(Baz* baz1, Baz* baz2)
{
    ...
    beSad();
    ...
}

I want to find any of these “bar” functions that have the word “beHappy” in them, using regular expressions. Keep in mind that these functions may call each other, and more generally, we don’t know what is in the “…”. It may even be that more than one of these functions has “beHappy” in them.

EDIT: If it is not possible due to C++ not being a regular language, please explain why, and, if possible, give a solution with it’s required restraints (such as, “there are no nested code blocks – using curly braces {} – in the code”).

  • 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-05-19T03:12:16+00:00Added an answer on May 19, 2026 at 3:12 am

    Pretending that every function has correct indentation (there are no blocks of code that belong to the function and have indentation that is less or equal to indentation of function definition):

    /\v(^\s*)regexForFunctionName\(.*[^;](\n\1\{)?(\n$|\n\1\s.*){-}\n\1\s.*\zs(whatToFind)/
    

    (braces around whatToFind are here only for a bit more readability). For your particular example regex will look like this:

    /\v(^\s*)[^(]*bar.*\(.*(\n\1\{)?(\n$|\n\1\s.*){-}\n\1\s.*\zsbeHappy/
    

    It is better to remove the only ? sign near (\n\1\{) in order to reduce probability that it captures code that is not a function definition. Depending on code style, more restrictions can be put on the part that ends with (\n\1\{)?. Note that if you use perl (and, probably, pcre), you can write an expression that will work with any valid C++ code, but this expression won’t definitely be regular.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.