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 8768349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:02:43+00:00 2026-06-13T17:02:43+00:00

I am trying to write a regexp to search for for/if/while keywords in a

  • 0

I am trying to write a regexp to search for for/if/while keywords in a string as read from C++ source code file but exclude any words which include them like:

WhatifYes()
Whatfor()
Waitforwhile()

I have written my regexp like below:

if { [ regexp {(for|while|if)(\s+)(\()} $lineValue ] } { 

But it is not picking up cases like:

while(( int x = 0 ) > 0 );
while(( int x = 0 ) > 0 )
for(int y =0 ; ; )
for(int y =0 ; ; );
if( (int x = 9) > 0 )
if( (int x = 9) > 0 );

Initially I thought cause my regexp is framed to be like:

if/for/while \s+ ( #space or multiple spaces

But I tried including spaces in above example :

while (( int x = 0 ) > 0 );
while (( int x = 0 ) > 0 )
if ( (int x = 9) > 0 )
if ( (int x = 9) > 0 );

Still the regexp is not working – please let me know what regexp I should use to capture them?

  • 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-13T17:02:44+00:00Added an answer on June 13, 2026 at 5:02 pm

    Part of your problem is easy to address, and part is very hard.

    The easy part is ensuring that you’ve got a whole word: the \m constraint escape only matches at the start of a word, and the \M constraint escape matches at the end, so we can use:

    # Nothing capturing; you can add that as necessary
    # Ellipsis for the bits I've not talked about yet
    regexp {\m(?:while|if|for)\M\s*...} ...
    

    The very hard part is matching the part in parentheses. The problem is that that’s really a “language” (in a theoretical sense) that requires a different kind of parser than a regular expression to match (i.e., a recursive descent parser, which has a more complex state model than the finite automatons used in RE matching). What’s more, using () characters in those expressions is common. The easiest approach is instead match against a close parenthesis that’s at the end of the line, possibly followed by a semicolon, but that’s definitely not properly correct. Alternatively, supporting a limited number of levels of nested parens is also possible.

    # Match a few levels...
    regexp {\m(?:while|if|for)\M\s*\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\)} ...
    

    So, let’s break that RE down:

    \m                                Word start
    (?:while|if|for)                  One of the keywords 
    \M                                Word end
    \s*                               Optional spaces
    \(                                Open paren
      (?:                             Either...
        [^()]                           Non-paren...
      |                               Or...
        \(                              Open paren
          (?:                           Either...
            [^()]                         Non-paren...
          |                             Or...
            \(                            Open paren
              [^()]*                      Non-parens
            \)                            Close paren
          )*                            ... as many of the above as needed
        \)                              Close paren
      )*                              ... as many of the above as needed
    \)                                Close paren
    

    If you look at the above, you’ll notice a pattern. Yes, you can keep on nesting to do as deep as you want. What you can’t do is make the RE engine do that nesting for you.

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

Sidebar

Related Questions

I’m trying to write a regex/method that extracts Variables from an input String that
I am trying to write a generic method that will search a file for
I am trying to extract the string from a file having following pattern within
I am trying to write a regex in Ruby to search a string for
I'm trying to write a javascript regexp for a blacklist of file extensions. I'm
I'm trying to write a Regex that that will extract individual fields from a
I am trying to write a regex to get the numbers from strings like
I'm trying to write a regex that will match everything BUT an apostrophe that
I'm trying to write a RegEx that starts with the number 0 or ANY
I'm trying to write a regex to match any path that contains /? to

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.