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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:22:52+00:00 2026-06-09T19:22:52+00:00

I was trying to make a script to allow me to automate clean ups

  • 0

I was trying to make a script to allow me to automate clean ups in the linux kernel a little bit. The first thing on my agenda was to remove braces({}) on if statements(c-styled) that wasnt necessary for single statement blocks. Now the code I tried with my little knowledge of regex in python I got to a working state, such as:

if (!buf || !buf_len) {
        TRACE_RET(chip, STATUS_FAIL);
        }

and the script turn it into:

if (!buf || !buf_len) 
        TRACE_RET(chip, STATUS_FAIL);

Thats what I want but when I try it on real source files it seems like it randomly selects a if statement and take its deleted it beginning brace and it has multiple statement blocks and it remove the ending brace far down the program usually on a else satement or a long if statement.

So can someone please help me with make the script only touch an if statement if it has a single block statement and correctly delete it corresponding beginning and ending brace.

The correct script looks like:

from sys import argv
import os
import sys
import re

get_filename = argv[1]
target = open(get_filename)
rename = get_filename + '.tmp'
temp = open(rename, 'w')

def if_statement():
    look=target.read()
    pattern=r'''if (\([^.)]*\)) (\{)(\n)([^>]+)(\})'''
    replacement=r'''if \1 \3\4'''
    pattern_obj = re.compile(pattern, re.MULTILINE)
    outtext = re.sub(pattern_obj, replacement, look)
    temp.write(outtext)
    temp.close()
    target.close()


if_statement()

Thanks in advance

  • 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-09T19:22:54+00:00Added an answer on June 9, 2026 at 7:22 pm

    In theory, this would mostly work:

    re.sub(r'(if\s*\([^{]+\)\s*){([^;]*;)\s*}', r'\1\2', yourstring)
    

    Note that this will fail on nested single-statement blocks and on semicolons inside string or character literals.

    In general, trying to parse C code with regex is a bad idea, and you really shouldn’t get rid of those braces anyway. It’s good practice to have them and they’re not hurting anything.

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

Sidebar

Related Questions

I am trying to make a simple script to automate and log synchronization via
I'm currently trying to make a GreaseMonkey script that will allow a user to
I am trying to make my pagination script a bit more dynamic and am
I'm trying to make a script that will prompt the user and wait for
I'm trying to make a script that automatically starts uploading after the data has
I'm trying to make a script that will go into a directory and run
I am trying to make a script to login into my check card balance
I'm trying to make simple script using jquery $post function to pass data to
I've been trying to make this script from ClearBox3 to stop conflicting with MooTools
hello everyone I am trying to make a script that steps through a list

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.