CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Help Needed for Pattern Matchin for Variable Seperator.

 



agutal
New User

May 9, 2009, 9:08 AM

Post #1 of 19 (12942 views)
Help Needed for Pattern Matchin for Variable Seperator. Can't Post

 i m writing a prg. for Code Analysis for C Code in which i need to
replace a particular variable wid the pattern.

Code is :
eg :
x=t+1;
p= malloc(x+1);
q = strlen(var,0);

Now i have to replace a particular variable such as "x" wid string "Taint" . how to write a separator code

such as
Taint = t+1;
p = malloc(Taint+1);
q=strlen(var,0);

can any one help me wid this.
This code analysis is for C variables replacement.

Thnks


vikas.deep
User

May 9, 2009, 11:10 PM

Post #2 of 19 (12931 views)
Re: [agutal] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

If you take the input as a plain text file then it can be done as follows

#This is the input file
#x=t+1;
#p= malloc(x+1);
#q = strlen(var,0);

<code>
#! /usr/local/bin/perl
use strict;
use warnings;
{
print "Enter the file name";
my $filename = <>;
chomp $filename;
open (my $FH, "$filename") or die "cannot open the file $filename: $!";
my @filecon = <$FH>;
foreach (@filecon)
{
if ($_ =~ /x/g)
{
$_ =~ s/x/Taint/g
}
}
print @filecon
}
</code>
#this is the output
#[root@localhost Documents]# perl un.pl
#Enter the file nametext
#Taint=t+1;
#p= malloc(Taint+1);
#q = strlen(var,0);
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


FishMonger
Veteran / Moderator

May 10, 2009, 9:08 AM

Post #3 of 19 (12908 views)
Re: [vikas.deep] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

vikas,

Please use the code tags buttons when posting code, not you own <code></code> delimiters.

Read:
perldoc -q quoting

Why do you slurp the file into an array? Just use a while loop to process the filehandle.

Why do you use 2 regex's? Only the substitution regex is needed and the g is not needed on your 1st regex.

Blindly substituting all 'x' is probably not what the OP wanted, but if it is, when this 1 liner is all that's needed.

Code
perl -pi 's/x/Taint/g' filename



agutal
New User

May 10, 2009, 10:22 AM

Post #4 of 19 (12903 views)
Re: [FishMonger] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

i needed for all the variable operations in C language.

For eg. int x;

suppose if i wanted to replace this x , even in complicated operations as if
<code>
o = (malloc(sizeof(x))+1);
</code>
when i m writing the code it is replacing it in.
<code>
o =malloc(Taint+1);
</code>
Please reply


vikas.deep
User

May 10, 2009, 9:56 PM

Post #5 of 19 (12894 views)
Re: [FishMonger] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Thank you for your comments and suggested improvements and/or corrections. As you guessed it right the person is back with the (continued) query.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

May 11, 2009, 1:23 AM

Post #6 of 19 (12884 views)
Re: [agutal] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

I don't think anybody can guess about the level of complexity you are working on unless you explicitly tell.
Further the example you have come up can be easily done with using the following
while (<$FH>)
{
if ($_ =~ /(.+(.+(x).*)\+\d+)/)
{$_ =~ s/$+/Taint/;
}
print "\n",$_

#This will give you the following
#[root@localhost Documents]# perl sur.pl

#o = (malloc(sizeof(Taint))+1);

but as you can see that this is a very specific solution to the specific question you put up and each time you have to design a new regex depending on the problem.
I guess you are searching for some general solution that can be applied to a wide variety of problems. Still if you mean
" for all the variable operations in C language"
It is beyond my knowledge--
Somebody else might suggest you a still better or elegant way to do it
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on May 11, 2009, 1:27 AM)


agutal
New User

May 14, 2009, 2:57 AM

Post #7 of 19 (12840 views)
Re: [agutal] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Thanks for the reply.
my major problem is :

Suppose there is sum string in a particular variable

my $var;


$var = "tainted";

i want to replace the only the occurence of complete word tainted with the input line.

and not "ttainted" or "aint" . but what i have riten gets matched wid this code.

Now consider that "tainted" is used in all arithmetic operations such as

a = tainted + 1;
y = (tainted)--;

and all i want to write pattern such tha only $var and exactly the contents of $var shud get replace wid it.

It will b very helpfull if u sort my problem.

Thanks in advance.

eg :

$t=p-$var;

m = memcpy($var,p,u);
and many such egs.


alex_v2
User

May 14, 2009, 3:42 AM

Post #8 of 19 (12839 views)
Re: [agutal] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

This might set you on the right path which is just a small variation of what most other people have posted.


Code
#!/usr/bin/perl 

use strict;

my $str = 'x=1.x.x';

$str =~ s/(^|\W|\b)x(\W|$|\b)/$1 . 'tainted' . $2/eg;

print qq($str\n);


-Alexander


vikas.deep
User

May 14, 2009, 5:08 AM

Post #9 of 19 (12838 views)
Re: [alex_v2] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Like perl language itself you are (very modestly) trying to brush gory details under the carpet.
my $str = 'x=1.x.x';
This is not "just a small variation " in fact my question is that What is this? I have never before seen such an initialisation. what is x=1.x.x My guess is "I can't guess!!" Are you trying to concatenate two x with a number 1 or what
Kindly explain
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


alex_v2
User

May 14, 2009, 8:10 AM

Post #10 of 19 (12833 views)
Re: [vikas.deep] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Ok original post said they wanted to be able to search for a string like x that does not match xx or xxx but only x in a complex string of characters. From the examples given I wrote the code and should have made one slight change to fully demonstrate it but hey it was late so... here is the code and a brief explanation.


Code
#!/usr/bin/perl 

use strict;

my $str = 'x=1.x.x=xxx+(x)x';

$str =~ s/(^|\W|\b)x(\W|$|\b)/$1 . 'tainted' . $2/eg;

print qq($str\n);


$str is just an example of a possible input line nothing more. if you run the above all instance of x will be replaced with taint but the instance of xxx will be untouched. SO in the regex all you have to do is replace x with what you want to find to get the kind of functionality desired.

-Alexander


1arryb
User

May 19, 2009, 8:11 AM

Post #11 of 19 (12744 views)
Re: [alex_v2] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Vikas,

Not bad (definitely more concise than my attempt!). However, you'll get some false positives from boolean operators. Try your regex against:

Code
for(x=1; x<foo;x++) { 
for( x=1; x<foo;x++ ) {
for( x = 1 ; x < foo; x ++ ) {
if ( x == 3 ) {


Also, (for alex), This kind of analysis can't be definitive unless you run the c code through the pre-processor first; otherwise, the code could be "tainted" indirectly via macro definitions including x.

UPDATE:

Here's the full test data I used:

Code
# Unindented 
# simple assignment.
x=1;
x =1;
x = 1;
x-=1;
x+=1;
x -= 1;
x += 1;
# Unary operators.
x++;
x--;
++x;
--x;
# indented;
x=1;
x =1;
x = 1;
x = 1;
x-=1;
x+=1;
x -= 1;
x += 1;
x++;
x--;
++x;
--x;
# false positives
xx=1;
xxx=1;
if (x == 3) {
if ( x < 3 ) {
# Loops and conditionals.
for(x=1; x<foo;x++) {
for( x=1; x<foo;x++ ) {
for( x = 1 ; x < foo; x ++ ) {


Cheers,

Larry


(This post was edited by 1arryb on May 19, 2009, 8:24 AM)


vikas.deep
User

May 28, 2009, 9:04 AM

Post #12 of 19 (12639 views)
Re: [1arryb] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Larry

I simply can't understand what is happening over here. As I have said that if one desires to solve the problem using regular expression then it can't be a general one and every time one will have to design a new one. I solved the case for some malloc(x) something and not for

Code
  for(x=1; x<foo;x++) {   
for( x=1; x<foo;x++ ) {
for( x = 1 ; x < foo; x ++ ) {
if ( x == 3 ) {

where it (regex) will fail obviously.

Then alex stepped in and it seems he created a new problem (Even after he explained from his side I lack a proper understanding)

Code
 my $str = 'x=1.x.x=xxx+(x)x';

and devised a solution to that problem(which he devised) That solution again cannot be applied to the original

Code
  malloc(x) problem

and when I tried that (alex's solution) on the original malloc problem or your test data
my computer says

Code
 C:\ActivePerl>perl larr.pl  
Use of uninitialized value $str in substitution (s///) at larr.pl line 8, <$FH> line 1.
Use of uninitialized value $str in concatenation (.) or string at larr.pl line 9, <$FH> line 1.

and then you put that (alex's)solution to a long list of test data how come you never got this error

Code
 C:\ActivePerl>perl larr.pl  
Use of uninitialized value $str in substitution (s///) at larr.pl line 8, <$FH> line 1.
Use of uninitialized value $str in concatenation (.) or string at larr.pl line 9, <$FH> line 1.

Alex's solution applies only to the case which he imagined/ mentioned in his post something like
my $str = 'x=1.x.x=xxx+(x)x';

I am a beginner in perl and I am not able to understand what is happening over here.

Why do I get a feeling that two grown-ups and/or more experienced persons over here are talking/discussing some serious matter even though all of it was how to sustitute "tainted" in some malloc(x) thing
Kindly explain a bit

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on May 28, 2009, 9:10 AM)


1arryb
User

May 28, 2009, 9:47 AM

Post #13 of 19 (12635 views)
Re: [vikas.deep] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Hi vikas,

The 'Use uf uninitialized...' messages are warnings, not errors. They come from the 'use warnings' pragma. These warnings are common in perfectly good Perl regular expression code. For example:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $str = 'foo';
$str =~ /(bar)/; # Doesn't match: $1 will be undefined.
print "wOOt!\n" if $1; # Perfectly ok, but results in the "Use of uninitialized..." warning.

The warning could have been avoided if I did something like:

Code
print "wOOt!\n" if (defined($1) && $1);

But this kind of coding gets old fast. Bottom line: These warnings are useful during development, but I frequently add:

Code
no warnings 'uninitialized';

To my programs after they are debugged to suppress them.

Cheers,

Larry


FishMonger
Veteran / Moderator

May 28, 2009, 10:30 AM

Post #14 of 19 (12630 views)
Re: [1arryb] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post


Quote
print "wOOt!\n" if $1; # Perfectly ok, but results in the "Use of uninitialized..." warning.


Are you sure? I don't think so.


Quote
[root@rkb-2 ~]# cat test.pl
#!/usr/bin/perl

use strict;
use warnings;

my $str = 'foo';
$str =~ /(bar)/; # Doesn't match: $1 will be undefined.
print "wOOt!\n" if $1; # Perfectly ok, but results in the "Use of uninitialized..." warning.


[root@rkb-2 ~]# ./test.pl
[root@rkb-2 ~]#



KevinR
Veteran


May 28, 2009, 11:00 AM

Post #15 of 19 (12628 views)
Re: [FishMonger] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Larrys example may have not been the best, but his comments are still true. Warnings are there to alert you to a potential problem and sometimes perfectly good code returns warnings.
-------------------------------------------------


1arryb
User

May 28, 2009, 11:38 AM

Post #16 of 19 (12625 views)
Re: [FishMonger] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Fish,

That was the expected output. Maybe my intent would have been clearer if I'd done:

Code
print "wOOt!\n" unless $1;


Larry


FishMonger
Veteran / Moderator

May 28, 2009, 11:47 AM

Post #17 of 19 (12624 views)
Re: [1arryb] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

But that won't produce the warning...maybe you meant this?

Code
print "wOOt! $1\n" unless $1;



FishMonger
Veteran / Moderator

May 28, 2009, 11:50 AM

Post #18 of 19 (12623 views)
Re: [1arryb] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Or, maybe you meant to leave off the conditional?

Code
print "wOOt! $1\n";



1arryb
User

May 28, 2009, 11:51 AM

Post #19 of 19 (12621 views)
Re: [FishMonger] Help Needed for Pattern Matchin for Variable Seperator. [In reply to] Can't Post

Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives