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 121013

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:54:09+00:00 2026-05-11T03:54:09+00:00

In my Python application, I need to write a regular expression that matches a

  • 0

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this:

for (int i = 0; i < 10; i++); 

… but not this:

for (int i = 0; i < 10; i++) 

This looks trivial at first glance, until you realise that the text between the opening and closing parenthesis may contain other parenthesis, for example:

for (int i = funcA(); i < funcB(); i++); 

I’m using the python.re module. Right now my regular expression looks like this (I’ve left my comments in so you can understand it easier):

# match any line that begins with a 'for' or 'while' statement: ^\s*(for|while)\s* \(  # match the initial opening parenthesis     # Now make a named group 'balanced' which matches a balanced substring.     (?P<balanced>         # A balanced substring is either something that is not a parenthesis:         [^()]         | # …or a parenthesised string:         \( # A parenthesised string begins with an opening parenthesis             (?P=balanced)* # …followed by a sequence of balanced substrings         \) # …and ends with a closing parenthesis     )*  # Look for a sequence of balanced substrings \)  # Finally, the outer closing parenthesis. # must end with a semi-colon to match: \s*;\s* 

This works perfectly for all the above cases, but it breaks as soon as you try and make the third part of the for loop contain a function, like so:

for (int i = 0; i < 10; doSomethingTo(i)); 

I think it breaks because as soon as you put some text between the opening and closing parenthesis, the ‘balanced’ group matches that contained text, and thus the (?P=balanced) part doesn’t work any more since it won’t match (due to the fact that the text inside the parenthesis is different).

In my Python code I’m using the VERBOSE and MULTILINE flags, and creating the regular expression like so:

REGEX_STR = r'''# match any line that begins with a 'for' or 'while' statement: ^\s*(for|while)\s* \(  # match the initial opening parenthesis     # Now make a named group 'balanced' which matches     # a balanced substring.     (?P<balanced>         # A balanced substring is either something that is not a parenthesis:         [^()]         | # …or a parenthesised string:         \( # A parenthesised string begins with an opening parenthesis             (?P=balanced)* # …followed by a sequence of balanced substrings         \) # …and ends with a closing parenthesis     )*  # Look for a sequence of balanced substrings \)  # Finally, the outer closing parenthesis. # must end with a semi-colon to match: \s*;\s*'''  REGEX_OBJ = re.compile(REGEX_STR, re.MULTILINE| re.VERBOSE) 

Can anyone suggest an improvement to this regular expression? It’s getting too complicated for me to get my head around.

  • 0 0 Answers
  • 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. 2026-05-11T03:54:10+00:00Added an answer on May 11, 2026 at 3:54 am

    You could write a little, very simple routine that does it, without using a regular expression:

    • Set a position counter pos so that is points to just before the opening bracket after your for or while.
    • Set an open brackets counter openBr to 0.
    • Now keep incrementing pos, reading the characters at the respective positions, and increment openBr when you see an opening bracket, and decrement it when you see a closing bracket. That will increment it once at the beginning, for the first opening bracket in ‘for (‘, increment and decrement some more for some brackets in between, and set it back to 0 when your for bracket closes.
    • So, stop when openBr is 0 again.

    The stopping positon is your closing bracket of for(...). Now you can check if there is a semicolon following or not.

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Now that the <a> is a block level element, you… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer I haven't got a specific example, but the Windows API… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer It looks like you've forgotten to append a null '\0'… May 12, 2026 at 12:16 pm

Related Questions

I'm writing a log collection / analysis application in Python and I need to
I am writing an application where one of the features is to allow the
I'm trying to write a video application in PyQt4 and I've used Python ctypes
I need to allow other Python applications to register callback functions for events in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.