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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:44:53+00:00 2026-05-16T17:44:53+00:00

I am trying to write a regex to solve the below C struct for

  • 0

I am trying to write a regex to solve the below C struct
for one of our requirement.
I am parsing the C structure file. In that I will have child and parent struct. The child will inherit parent struct members. I want the output, which consists of struct members and there length for further processing

INPUT:

#define  Len1  10;
#define  Len2  20;
#define  Len3   3;
#define  Len4   4;

typedef struct
{
char CHAR1           [Len1];
char CHAR2           [Len2];
} MyParent;


typedef struct
{

MyParent base;
char CHAR3        [Len3];
char CHAR4          [Len4];

} MyChild;

In the above I have to get: below OUTPUT:

 CHAR1 10
 CHAR2 20
 CHAR3 3
 CHAR4 4

The Perl Script will be really helpfull;

  • 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-16T17:44:54+00:00Added an answer on May 16, 2026 at 5:44 pm

    You changed the problem after I gave my answer. It’s not big of a deal though because the answer is not that much more complicated. You remember what you see in the #defines and use them later:

    while( <DATA> ) {
        if( /^#define\s+(\w+)\s+(\d+)/ ) {
            $Len{$1} = $2;
            }
        elsif( /^char\s/ ){
            s/(?<=CHAR\d)\s+//;
            s/;$//;
            s/(?<=\[)(\w+)(?=])/ $Len{$1} || $1 /e;
            print;
            }
        }
    

    This is a pretty trivial problem. Are you sure there’s something that you aren’t telling us? You don’t need to do anything fancy. Skip all the lines that you want to ignore, and fixup the remaining lines to match your output needs:

    while( <DATA> ) {
        next unless /^char\s/;
        s/(?<=CHAR\d)\s+//;
        s/;$//;
        print;
        }
    
    __DATA__
    #define  Len1  10;
    #define  Len2  20;
    #define  Len3   3;
    #define  Len4   4;
    
    typedef struct
    {
    char CHAR1           [Len1];
    char CHAR2           [Len2];
    } MyBase;
    
    
    typedef struct
    {
    
    MyBase base;
    char CHAR3          [Len3];
    char CHAR4          [Len4];
    
    } MyStruct;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a regex (in JavaScript) that will match a multi
I'm trying to write a Regex that that will extract individual fields from a
I'm trying to write a regex that will match everything BUT an apostrophe that
I am trying to write a regex that will allow me to parse CSV
I'm trying to write a regex pattern that will find numbers with two leading
I'm trying to write a regex that will remove HTML tags around a placeholder
I'm trying to write a regex expression that will take this phrase [[32, 120],
I'm trying to write a regex that will take a string of the form:
I have a string that looks like: Pretext<thecontentineed> I' trying to write a regex
I'm trying to write a regex function that will identify and replace a single

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.