Hi All
Today only i joined this group. need ur help.
Here goes my question:
Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched section?
I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions.
for ex:
#ifdef X
some code
#endif
#ifdef Y
somecode
#ifdef Z
somemorecode
#else
code that does not match condition
#endif
#endif
---------------------------------
from the above text, i have to extract sections between #ifdef and #endif.( and ofcourse #else in some cases)
let me know for any further explanation about the same.
thanks in adv
Pramod
6 4498 numberwhun 3,509
Recognized Expert Moderator Specialist
Hi All
Today only i joined this group. need ur help.
Here goes my question:
Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched section?
I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions.
for ex:
#ifdef X
some code
#endif
#ifdef Y
somecode
#ifdef Z
somemorecode
#else
code that does not match condition
#endif
#endif
---------------------------------
from the above text, i have to extract sections between #ifdef and #endif.( and ofcourse #else in some cases)
let me know for any further explanation about the same.
thanks in adv
Pramod
Well, considering this is a learning forum and not a script writing service, I am obligated to ask you what you have tried thus far.
Please post your code that you have tried, in the necessary code tags, and we will help guide you from there.
Regards,
Jeff
To get you started, you can extract elements from a perl match using parentheses on the left hand side and $ variables on the right
for instance -
$string="what is it"
-
print "$string\n";
-
$string =~ /^.* (is) .*$/$1/;
-
print "$string\n";
-
first print should get you "what is it"
second print should just be "is"
Hope this helps get you started!
, Mike
Here is what i hav tried so far.
1. Get the C file contents into array
2. Read elements of array(i.e lines of file)
3. check if it matches with '#ifdef and #endif'
4. set the flags( ifdefflag and endifflag)
5. take actions accordingly ( i mean write contents of ifdef into a temp file excluding #ifedf and #endif)
This is becoming complex to include all conditions like ( to check for #else and nested #ifdef )
Let me know is there any simple solution to achieve this. flag setting doesnt seem to be so useful in some cases.
Thanks
Pramod
numberwhun 3,509
Recognized Expert Moderator Specialist
The pseudo code that you supplied is not your actual code. Please provide your code, surrounded by the necessary code tags, and we will try and help you.
Regards,
Jeff
Hi,
Here is the code...need to format and change some of the declarations.
let me know if you need anu further comments to understand the code. -
# Global variable Declaration and Initialization
-
my $ifdefflag = 0;
-
my $elseflag = 0;
-
my $endifflag = 0;
-
my $counter = 0;
-
my $flag=0;
-
my $ctrToLookForifdef = 0;
-
my @filecontents;
-
my @splititem;
-
my @compiler_switches =(A,B,C); #has list of compiler switches used with #ifdef
-
-
#### Open File for reading the contents
-
open FH, "source.c" or die "can not open source.c...$!";
-
while (<FH>) {
-
@filecontents = <FH>;
-
}
-
#### Backing up source file using system command
-
`copy source.c source.bak`;
-
#### Create new file after refactoring
-
open NEWFH, ">source.c" or die "can not open source.c...$!";
-
-
### Read contents of array, Match for ifdef and set the flags
-
foreach my $item (@filecontents ){
-
# Match for #ifdef tag and set the flag
-
if($item =~m/#ifdef/) {
-
$endifflag = 0;
-
# couter to track nested ifdef
-
$ctrToLookForifdef++;
-
## splitting with space to check for valid compiler switch
-
@splititem= split(" ", $item);
-
foreach my $cs (@compiler_switches){
-
chomp $cs;
-
chomp $splititem[1];
-
if( $splititem[1] eq $cs and ($ctrToLookForifdef == 1)){
-
$ifdefflag = 1;
-
$flag=0;
-
last;
-
}
-
else {
-
$flag=1;
-
}
-
}
-
next;
-
}
-
elsif( $item =~m/#endif/){
-
$ctrToLookForifdef--;
-
if($ctrToLookForifdef == 0){
-
$endifflag = 1;
-
$ifdefflag = 0;
-
$flag=0;
-
}
-
next;
-
}
-
else {
-
if($flag==1){
-
next;
-
}
-
print NEWFH "$item";
-
next;
-
}
-
if($ifdefflag and $endifflag==0){
-
print NEWFH "$item";
-
}
-
if($elseflag and $flag){
-
print NEWFH "$item";
-
}
-
if($endifflag){
-
$ifdefflag = 0;
-
next;
-
}
-
}
-
close FH;
-
close NEWFH;
-
###### end of the file
-
Regards
Pramod
Hi All,
Any updates on this post? let me know if you need any further details.
I am still waiting for replies.
Regards
Pramod
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ravi |
last post by:
Hi,
I have about 200GB of data that I need to go through and extract the
common first part of a line. Something like this.
>>>a = "abcdefghijklmnopqrstuvwxyz"
>>>b = "abcdefghijklmnopBHLHT"
>>>c = extract(a,b)
>>>print c
"abcdefghijklmnop"
|
by: Follower |
last post by:
Hi,
I am working on a function to return extracts from a text document
with a specific phrase highlighted (i.e. display the context of the
matched phrase).
The requirements are:
* Match should be case-insensitive, but extract should have case
preserved.
|
by: Matt Wette |
last post by:
Over the last few years I have converted from Perl and Scheme to
Python. There one task that I do often that is really slick in Perl
but escapes me in Python. I read in a text line from a file and check
it against several regular expressions and do something once I find a match.
For example, in perl ...
if ($line =~ /struct {/) {
do something
} elsif ($line =~ /typedef struct {/) {
do something else
|
by: cricfan |
last post by:
I'm parsing a text file to extract word definitions. For example the
input text file contains the following content:
di.va.gate \'di_--v*-.ga_-t\ vb
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT
I am trying to obtain words between two literal backslashes (\ .. \). I
am not able to match words between two literal backslashes using the
regxp - re.compile(r'\\*\\').
|
by: David Moore |
last post by:
Hi All,
I expect someone can crack this one in no time.
Ok, what I'd like to do is to match a pattern in a string and extract a
portion of it. For example if I had a string like:
'The store had apples(20) and pears(30)'
how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)', how
do I expect the '??'.
| |
by: sherifffruitfly |
last post by:
Hi,
I've been searching as best I can for this - coming up with little.
I have a file that is full of lines fitting this pattern:
(?<year>\d{4}),(?<amount>\d{6,7})
I'm likely to get a bunch of hits with this - I'm only interested in
the *last* one. Is there a way to build the concept "last" into the
|
by: wo_shi_big_stomach |
last post by:
Newbie to python writing a script to recurse a directory tree and delete
the first line of a file if it contains a given string. I get the same
error on a Mac running OS X 10.4.8 and FreeBSD 6.1.
Here's the script:
# start of program
# p.pl - fix broken SMTP headers in email files
#
|
by: hari.siri74 |
last post by:
Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.
|
by: manishabh77 |
last post by:
I will be obliged if anybody can help me with this problem:
I am trying to extract data from an excel sheet that matches IDs given in column 4 of the excel sheet.I have stored those query IDs in an array (@names). After I look for the match in this section of the code:
if ($value=~/^$names$/), I want to write out only those rows that satisfy the above natch condition. But currently the code I have here writes out everything. How do I get it to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |