'extracting' from a string via regular expressions?

Discussion in 'Perl Misc' started by sseelenluft@gmail.com, Oct 13, 2008.

  1. Guest

    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.
     
    , Oct 13, 2008
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Oct 13, 2008
    #2
    1. Advertising

  3. Guest

    > > $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.)
     
    , Oct 13, 2008
    #3
  4. Klaus Guest

    On Oct 13, 4:53 pm, wrote:
    > $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";
    }
     
    Klaus, Oct 13, 2008
    #4
  5. wrote:
    >$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
     
    Jürgen Exner, Oct 13, 2008
    #5
  6. smallpond Guest

    On Oct 13, 10:53 am, wrote:


    > @ARGV = qw# test2.csv #;


    Wouldn't open be simpler? That way you can actually check
    whether it worked.
     
    smallpond, Oct 13, 2008
    #6
  7. <> wrote:

    > # 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".


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 13, 2008
    #7
  8. Guest

    On Oct 13, 6:08 pm, Tad J McClellan <> wrote:
    > <> wrote:
    > > # 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".

    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.
     
    , Oct 13, 2008
    #8
  9. Tim Greer Guest

    wrote:

    > 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@.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 14, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    610
    Jay Douglas
    Aug 15, 2003
  2. chad
    Replies:
    4
    Views:
    3,829
  3. David Krmpotic

    Regular expressions (extracting urls)

    David Krmpotic, Feb 5, 2007, in forum: Ruby
    Replies:
    4
    Views:
    113
    Robert Klemme
    Feb 7, 2007
  4. Kimi
    Replies:
    10
    Views:
    244
    John Bokma
    Nov 23, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page