473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Pathern Matching problem

Hi ,

I am writing script using multiple pattern matching.
I need to replace the $_ =~ few times.

Example:

sub xx {

open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine

xx ();

The problem is that the $_ doesn't execute the pattern matching.

What am I doing wrong?

Thanks a lot,
Jul 19 '05 #1
7 3526
Niko wrote:
I am writing script using multiple pattern matching.
I need to replace the $_ =~ few times.

Example:

sub xx {

open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine
Then you don't have a pattern matching problem at all, right?

But one problem you have is that you run your code without warnings
enabled: $1 etc. is prefered before \1 etc. at the right side of the
s/// operator.
The problem is that the $_ doesn't execute the pattern matching.


So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
Gunnar Hjalmarsson wrote:
Niko wrote:

while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine


Then you don't have a pattern matching problem at all, right?


Or maybe you have... What do you think happens if there are more than
one occurrences of <RE>something</RE> at the same line?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #3
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<CH*********************@newsc.telia.net>...
Niko wrote:
I am writing script using multiple pattern matching.
I need to replace the $_ =~ few times.

Example:

sub xx {

open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine


Then you don't have a pattern matching problem at all, right?

But one problem you have is that you run your code without warnings
enabled: $1 etc. is prefered before \1 etc. at the right side of the
s/// operator.
The problem is that the $_ doesn't execute the pattern matching.


So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant


HI,

First thanks for replay,
I did try the eval but it didnt work,
can you give me an example how to use it in my case ?

Thanks again.
Jul 19 '05 #4
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant
I did try the eval but it didnt work,


Then there is probably some other error in you program.
can you give me an example how to use it in my case ?


If it "does not work" for you, show us a short but *complete* program
with sample data that we can copy and run, where strictures and
warnings have been enabled, and that does not output the expected result.

If you do, we can help you fix it.

Another thing is that I'm not sure this eval() approach is a good
choice. It's probably better to use the qr// operator.

my $re = qr|(<RE>).*?(</RE)|;

s/$re/$1xxxx$2/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #5
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Du*********************@newsc.telia.net>...
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant


I did try the eval but it didnt work,


Then there is probably some other error in you program.
can you give me an example how to use it in my case ?


If it "does not work" for you, show us a short but *complete* program
with sample data that we can copy and run, where strictures and
warnings have been enabled, and that does not output the expected result.

If you do, we can help you fix it.

Another thing is that I'm not sure this eval() approach is a good
choice. It's probably better to use the qr// operator.

my $re = qr|(<RE>).*?(</RE)|;

s/$re/$1xxxx$2/g;


Hi ,

This is the full script:

#!/bin/perl
###############
# Global Vars #
###############
#

$OV_CONF_FILE = "/Users/conf.xml";
$UNINST = $ARGV[0];

#############
# Subroutins #
#############
#
sub Replace_Sub {
open (FILE,$S_FILE) || die "cant open file";
open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== this is the problem
print TEMPF $_;
}
close (TEMPF);
close (FILE);
}

########
# Main #
########
#

## Update loopback to on in ov.conf file - DR-DR-0-026-370
$S_FILE = $OV_CONF_FILE;
$T_FILE = "$S_FILE.new_sec_kit";
$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern

if ( $UNINST eq "remove") {
if (! -f "$S_FILE.orig_sec_kit") {
print "The $S_FILE Original file exist or already in orig state ...\n"
}else{
`mv -f $S_FILE.orig_sec_kit $S_FILE`;
}
}else{
if (-f "$S_FILE.orig_sec_kit") {
print "Modified $S_FILE file already exist....\n"
}else{
Replace_Sub();
`cp -p $S_FILE $S_FILE.orig_sec_kit`;
`mv -f $T_FILE $S_FILE`;
}
}

The content of the file is:

..
<RE>blabla</RE>
..
..

That's all.

Tahnks a lot.
Jul 19 '05 #6
Niko wrote:
Gunnar Hjalmarsson wrote:
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant

I did try the eval but it didnt work,


Then there is probably some other error in you program.


$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern

----------^-----------------------------^

Replace those double quotes with single quotes.

There is a lot more to say about your program, but I'm not in the
mood, since you ignored the advices I already gave you.

If you want to learn Perl, start here:

http://learn.perl.org/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #7
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant

I did try the eval but it didnt work,

What do you mean, "didnt work". You need to be more specific
to get any help here.
Another thing is that I'm not sure this eval() approach is a good
choice. It's probably better to use the qr// operator.

my $re = qr|(<RE>).*?(</RE)|;
s/$re/$1xxxx$2/g;

Did you try that?
This is the full script:

#!/bin/perl
You did not put in
use strict;
use warnings;
as Gunnar asked. You must do this if you seriously want assistance.
$_ =~ $string; <== this is the problem
That is not "a *complete* program with sample data".
What you posted doesn't even compile.
$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern
Inside a double-quoted string, "\1" and "\2" are *not* the
same as what is used in a regex. Two people have told you
things you can do instead, but you have ignored their advice. Why?

Learn how to use qr() or eval($perl_command).
The content of the file is:
.
<RE>blabla</RE>


You should not be describing the file in English.
You should include the file verbatim after the __DATA__ delimiter.
-Joe
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi, I'm fairly new to regular expressions, and this may be a rather dumb question, but so far I haven't found the answer in any tutorial or reference yet... If I have f.i. the string "The...
17
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of...
0
by: Eric A. Hall | last post by:
I'm trying to scratch up a whois-like client in perl, and am having a strange problem with matching input characters. Specifically, matching against things like "@" works fine over the network, but...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: grego9 | last post by:
I have a bit of visual basic code in an excel spreadsheet that I need some help with. I am attempting to search a file called TO Cancellations2.xls for counterparty names and for each counterparty...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.