'extracting' from a string via regular expressions?

S

sseelenluft

Hi,
I was modifying code written by others when something stopped working.
I was using a construct like:
$string=~ m@PartOne(.*)PartTwo@;
to find and extract everything between 'PartOne' and 'PartTwo', and I
swear it was working. Then I moved to a different string and it
stopped working (and now the first example also stopped working).
Below is a not-working test-case:

#! /usr/bin/perl

use strict;

# Definition of variables
my $line;
my $file;

# Read file
@ARGV = qw# test2.csv #;
while (defined($line = <>)) {
$file .= $line;
}

$file=~ m@TEMPLATES(.*)ONLY@;

print $file . "\n";

The file test2.csv contains just one sentence: 'IMPORTANT: THESE
TEMPLATES MAY ONLY BE USED WITH THE WRITTEN PERMISSION'
Therefore, the code above should print simply 'MAY' (possibly with the
spaces), but it prints the whole sentence.

Finding the word 'TEMPLATES' works fine, a la:
if ($file=~ m@TEMPLATES@) {
print "It matched!" . "\n";
}
works fine.

So, what am I missing here?
Thanks.
 
J

Josef Moellers

Hi,
I was modifying code written by others when something stopped working.
I was using a construct like:
$string=~ m@PartOne(.*)PartTwo@;
to find and extract everything between 'PartOne' and 'PartTwo', and I
swear it was working. Then I moved to a different string and it
stopped working (and now the first example also stopped working).
Below is a not-working test-case:

#! /usr/bin/perl

use strict;

# Definition of variables
my $line;
my $file;

# Read file
@ARGV = qw# test2.csv #;
while (defined($line = <>)) {
$file .= $line;
}

$file=~ m@TEMPLATES(.*)ONLY@;

This expression matches the contents of $file against the given RE.
It does not modify $file.
print $file . "\n";

print $1, "\n";

Josef
 
S

sseelenluft

$file=~ m@TEMPLATES(.*)ONLY@;
This expression matches the contents of $file against the given RE.
It does not modify $file.
Ah, your are right, when I thought it was working (ie, when I thought
this expression really extracts the content), I was inadvertently also
doing a print of another variable which had $1 assigned to itself, and
I had thought I was only printing $file (and because of the really
long output I only looked at the last lines).
Thanks.
(I guess, I should do a commit to a version control system every time
I hit the save button, otherwise I think something was working when it
fact something else was working.)
 
K

Klaus

$file=~ m@TEMPLATES(.*)ONLY@;
print $file . "\n";

if ($file=~ m@TEMPLATES(.*)ONLY@) {
print "found '$1' in file '$file'\n";
}
else {
print "nothing found in file '$file'\n";
}
 
J

Jürgen Exner

$file=~ m@TEMPLATES(.*)ONLY@;
print $file . "\n";
The file test2.csv contains just one sentence: 'IMPORTANT: THESE
TEMPLATES MAY ONLY BE USED WITH THE WRITTEN PERMISSION'
Therefore, the code above should print simply 'MAY' (possibly with the
spaces), but it prints the whole sentence.

You need to tell Perl to print the matched part only instead of the
whole line. And you should use the $-number variables only if the match
was successfull:

if ($file=~ m@TEMPLATES(.*)ONLY@) {
print "$1\n";
}


jue
 
T

Tad J McClellan

# Definition of variables
my $line;
my $file;


You are not defining variables there.

You are declaring variables there.

"defining" in not the same thing as "declaring".
 
S

sseelenluft

You are not defining variables there.

You are declaring variables there.

"defining" in not the same thing as "declaring".
You are correct, the original code was written by an Italian speaking
person, and I as a German speaker was not apparently not bothered
enough by it to change it.
 
T

Tim Greer

Hi,
I was modifying code written by others when something stopped working.
I was using a construct like:
$string=~ m@PartOne(.*)PartTwo@;
to find and extract everything between 'PartOne' and 'PartTwo', and I
swear it was working. Then I moved to a different string and it
stopped working (and now the first example also stopped working).
Below is a not-working test-case:

#! /usr/bin/perl

use strict;

# Definition of variables
my $line;
my $file;

# Read file
@ARGV = qw# test2.csv #;
while (defined($line = <>)) {
$file .= $line;
}

$file=~ m@TEMPLATES(.*)ONLY@;

print $file . "\n";

The file test2.csv contains just one sentence: 'IMPORTANT: THESE
TEMPLATES MAY ONLY BE USED WITH THE WRITTEN PERMISSION'
Therefore, the code above should print simply 'MAY' (possibly with the
spaces), but it prints the whole sentence.

Finding the word 'TEMPLATES' works fine, a la:
if ($file=~ m@TEMPLATES@) {
print "It matched!" . "\n";
}
works fine.

So, what am I missing here?
Thanks.

Did you mean: $file =~ s@TEMPLATES(.*)ONLY@$1@;

I won't get into the other issues at this point, but it looks like
that's your problem. You were only looking for a match (in which case
you don't need to (capture) the match), then perhaps you meant to say
$file = $1 if $file =~ m@TEMPLATES(.*)ONLY@.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top