cannot match pattern

Discussion in 'Perl Misc' started by Domenico Discepola, Dec 1, 2003.

  1. Hello. I cannot match $ex in the following example. I'm reading records
    from a text file (field seperator "\n" record seperator "\x0c"). I
    simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
    able to get any insight from perldoc... Any help would be appreciated.

    #!perl
    use strict;
    use warnings;

    my $ex = '$Conflict: ';
    my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    print "[$a]\n";

    if ( $a =~ /$ex/ ) {
    print "found\n";
    } else {
    print "not found\n";
    }
    Domenico Discepola, Dec 1, 2003
    #1
    1. Advertising

  2. Domenico Discepola

    Ben Morrow Guest

    "Domenico Discepola" <> wrote:
    > Hello. I cannot match $ex in the following example. I'm reading records
    > from a text file (field seperator "\n" record seperator "\x0c"). I
    > simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
    > able to get any insight from perldoc... Any help would be appreciated.
    >
    > #!perl
    > use strict;
    > use warnings;
    >
    > my $ex = '$Conflict: ';
    > my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    > print "[$a]\n";
    >
    > if ( $a =~ /$ex/ ) {
    > print "found\n";
    > } else {
    > print "not found\n";
    > }


    Have you run this code? It works for me.

    As a side issue, it's best not to use $a or $b: see perlvar.

    Ben

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    - Heraclitus
    Ben Morrow, Dec 1, 2003
    #2
    1. Advertising

  3. "Ben Morrow" <> wrote in message
    news:bqg0ri$rqt$...
    >
    > "Domenico Discepola" <> wrote:
    > > Hello. I cannot match $ex in the following example. I'm reading

    records
    > > from a text file (field seperator "\n" record seperator "\x0c"). I
    > > simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
    > > able to get any insight from perldoc... Any help would be appreciated.
    > >
    > > #!perl
    > > use strict;
    > > use warnings;
    > >
    > > my $ex = '$Conflict: ';
    > > my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    > > print "[$a]\n";
    > >
    > > if ( $a =~ /$ex/ ) {
    > > print "found\n";
    > > } else {
    > > print "not found\n";
    > > }

    >
    > Have you run this code? It works for me.
    >
    > As a side issue, it's best not to use $a or $b: see perlvar.
    >
    > Ben
    >


    Yes - I ran this code on my w2k and redhat boxes and perl still does not
    match the expression...
    Point taken on the $a $b vars...
    Domenico Discepola, Dec 1, 2003
    #3
  4. Domenico Discepola <> wrote:

    > I cannot match $ex in the following example.


    > my $ex = '$Conflict: ';


    > if ( $a =~ /$ex/ ) {
    > print "found\n";



    Your pattern says that you want to match "Conflict: " _after_
    the end of the string.

    That is never going to happen.

    my $ex = '\$Conflict: ';

    or

    if ( $a =~ /\Q$ex/ ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 1, 2003
    #4
  5. Domenico Discepola

    Ben Morrow Guest

    "Domenico Discepola" <> wrote:
    > "Ben Morrow" <> wrote in message
    > news:bqg0ri$rqt$...
    > >
    > > "Domenico Discepola" <> wrote:
    > > > Hello. I cannot match $ex in the following example. I'm reading

    > records
    > > > from a text file (field seperator "\n" record seperator "\x0c"). I
    > > > simulated 1 incoming record in the $a variable. Unfortunately, I wasn't
    > > > able to get any insight from perldoc... Any help would be appreciated.
    > > >
    > > > #!perl
    > > > use strict;
    > > > use warnings;
    > > >
    > > > my $ex = '$Conflict: ';
    > > > my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    > > > print "[$a]\n";
    > > >
    > > > if ( $a =~ /$ex/ ) {
    > > > print "found\n";
    > > > } else {
    > > > print "not found\n";
    > > > }

    > >
    > > Have you run this code? It works for me.

    >
    > Yes - I ran this code on my w2k and redhat boxes and perl still does not
    > match the expression...


    My apologies: I am an idiot. I made the reverse of the usual mistake
    and retyped my test, with a typo... :(

    The trouble is that your regex comes out as /$Conflict: /, which
    attempts to match 'end-of-string, followed by Conflict: '. This
    doesn't match. What you want is

    if ( $a =~ /\Q$ex/ ) {

    which will quote the $ for you. \Q is documented in perlop, under
    "Quote and Quote-like Operators".

    Ben

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
    Ben Morrow, Dec 1, 2003
    #5
  6. Domenico Discepola <> wrote:
    > my $ex = '$Conflict: ';
    > my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    > print "[$a]\n";
    >
    > if ( $a =~ /$ex/ ) {


    The dollar sign in $ex is the problem. Perl's trying to match
    'Conflict: ' after the end of the line.

    use: $ex = '\$Conflict: ';
    or: $ex = quotemeta '$Conflict: ';
    or: $a =~ /\Q$ex/

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Dec 1, 2003
    #6
  7. Domenico Discepola

    gilgames Guest

    my $ex = '\$Conflict: ';
    my $a = "field1: test1\n\$Conflict: 1\nfield 3: test3\x0c";
    print "[$a]\n";

    if ( $a =~ /$ex/ ) {
    print "found\n";
    } else {
    print "not found\n";}

    This is the correct. The change is in the first line here :

    my $ex = '\$Conflict: ';

    This way returns

    [field1: test1
    $CONFLICT: 1
    field 3: test3 ]
    found
    gilgames, Dec 1, 2003
    #7
    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. hiwa
    Replies:
    0
    Views:
    628
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    463
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    154
Loading...

Share This Page