regex trick needed

Discussion in 'Perl Misc' started by Patrick Drouin, Oct 26, 2004.

  1. Hello guys,

    I've browsed the web, looked at the camel book and asked around but I
    haven't been able to solve what seems to be a simple problem. I want to
    load regex on the fly from an external file, that's not a big thing.

    The problem I am running into is that these regex used patterns I have
    described somewhere else in my program and stored into scalars. Here's
    the thing :

    Defined in my program (to simplify regex writing by the users):
    $nom = '[^\/]+\/(?:proper|Common)_Noun\/[^ ]+';
    $adj = '[^\/]+\/Adjective\/[^ ]+';
    .....

    I now have these rules outside:
    \s$nom
    \s$nom (?:$adj)+

    I use them this way :
    if(@matches = $line =~ /$regex/g) {save_list(@matches);print "Yup!\n";}
    else {print "Nope!\n";}

    I'm not getting any "Yup!" back.... It seems that the internal $nom
    scalar is not expanded to its definition in the pattern. Is there anyway
    to force Perl to do this in a regex?

    I've tried defining the first rule as this in the external file and it
    works just fine :

    \s[^\/]+\/(?:proper|Common)_Noun\/[^ ]+

    Any help will be appreciated.

    Patrick
     
    Patrick Drouin, Oct 26, 2004
    #1
    1. Advertising

  2. Patrick Drouin

    Paul Lalli Guest

    "Patrick Drouin" <> wrote in message
    news:d2xfd.111835$...

    > I've browsed the web, looked at the camel book and asked around but I
    > haven't been able to solve what seems to be a simple problem. I want

    to
    > load regex on the fly from an external file, that's not a big thing.
    >
    > The problem I am running into is that these regex used patterns I

    have
    > described somewhere else in my program and stored into scalars. Here's
    > the thing :
    >
    > Defined in my program (to simplify regex writing by the users):
    > $nom = '[^\/]+\/(?:proper|Common)_Noun\/[^ ]+';
    > $adj = '[^\/]+\/Adjective\/[^ ]+';
    > ....
    >
    > I now have these rules outside:
    > \s$nom
    > \s$nom (?:$adj)+
    >
    > I use them this way :
    > if(@matches = $line =~ /$regex/g) {save_list(@matches);print

    "Yup!\n";}
    > else {print "Nope!\n";}
    >
    > I'm not getting any "Yup!" back.... It seems that the internal $nom
    > scalar is not expanded to its definition in the pattern. Is there

    anyway
    > to force Perl to do this in a regex?


    I can't parse most of what you've said (and you didn't provide a
    short-but-complete script that demonstrates your problem), but this
    sentence is a red-flag. Have you read the entry titled "How can I
    expand variables in text strings?" in the Perl FAQ?

    perldoc -q expand

    If that doesn't solve your problem, please post a short-but-complete
    script that demonstrates your problem. Perl is usually easier to
    understand than English. (of course, the best explanation is both Perl
    and English...)

    Paul Lalli
     
    Paul Lalli, Oct 26, 2004
    #2
    1. Advertising

  3. Patrick Drouin

    Matija Papec Guest

    X-Ftn-To: Patrick Drouin

    Patrick Drouin <> wrote:
    >Defined in my program (to simplify regex writing by the users):
    >$nom = '[^\/]+\/(?:proper|Common)_Noun\/[^ ]+';
    >$adj = '[^\/]+\/Adjective\/[^ ]+';
    >....
    >
    >I now have these rules outside:
    >\s$nom
    >\s$nom (?:$adj)+
    >
    >I use them this way :
    >if(@matches = $line =~ /$regex/g) {save_list(@matches);print "Yup!\n";}
    >else {print "Nope!\n";}
    >
    >I'm not getting any "Yup!" back.... It seems that the internal $nom


    I would first print $line and $regex before your condition.



    --
    Matija
     
    Matija Papec, Oct 26, 2004
    #3
  4. Patrick Drouin wrote:
    >
    > Defined in my program (to simplify regex writing by the users):
    > $nom = '[^\/]+\/(?:proper|Common)_Noun\/[^ ]+';
    > $adj = '[^\/]+\/Adjective\/[^ ]+';
    > ....
    >
    > I now have these rules outside:
    > \s$nom
    > \s$nom (?:$adj)+
    >
    > I use them this way :
    > if(@matches = $line =~ /$regex/g) {save_list(@matches);print "Yup!\n";}
    > else {print "Nope!\n";}
    >
    > I'm not getting any "Yup!" back.... It seems that the internal $nom
    > scalar is not expanded to its definition in the pattern. Is there anyway
    > to force Perl to do this in a regex?


    Maybe you want to preprocess $regex before using it in the regex.

    perldoc -q "expand variables"

    If that doesn't help, please post a short but *complete* program that
    illustrates what you are trying to do.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 26, 2004
    #4
  5. Although the description of my problem was not very clear, you guys
    pointed me in the right direction. Here's what I used:

    $rule =~ s/\$(\w+)/${$1}/g;

    This expans the variable used inside the rule I read from text files.

    Thanks,
    Patrick
     
    Patrick Drouin, Oct 26, 2004
    #5
  6. Patrick Drouin

    Ben Morrow Guest

    Quoth :
    > Although the description of my problem was not very clear, you guys
    > pointed me in the right direction. Here's what I used:
    >
    > $rule =~ s/\$(\w+)/${$1}/g;
    >
    > This expans the variable used inside the rule I read from text files.


    Gah, yuck!

    Why haven't you got strictures on?

    Use a hash of common patterns instead:

    my %Patterns = (
    pnoun => qr/(?:Common\s+)Noun/,
    );

    my $rule = get_rule_from_file;

    $rule =~ s/\$(\w+)/$Patterns{$1}/g;

    And if you're going to be matching these a lot, it's probably worth
    compiling them:

    $rule = qr/$rule/;

    Ben

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Oct 26, 2004
    #6
  7. Patrick Drouin wrote:
    > Although the description of my problem was not very clear, you guys
    > pointed me in the right direction. Here's what I used:
    >
    > $rule =~ s/\$(\w+)/${$1}/g;
    >
    > This expans the variable used inside the rule I read from text files.


    If that made it, you are probably not using strict. Bad! :(

    A better solution is indicated at the *end* of

    perldoc -q "expand variables"

    Why not try that, and start using strictures and warnings, and become a
    better Perl programmer? ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 26, 2004
    #7
  8. Patrick Drouin <> wrote:
    > Although the description of my problem was not very clear, you guys
    > pointed me in the right direction. Here's what I used:
    >
    > $rule =~ s/\$(\w+)/${$1}/g;



    You should put "use strict;" in all of your Perl programs!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 27, 2004
    #8
  9. Tad McClellan <> writes:
    > Patrick Drouin <> wrote:
    > > Although the description of my problem was not very clear, you guys
    > > pointed me in the right direction. Here's what I used:
    > >
    > > $rule =~ s/\$(\w+)/${$1}/g;

    >
    >
    > You should put "use strict;" in all of your Perl programs!


    Sorry for being dense, but what would "use strict" complain about in
    the above statement? Putting it inside a plausible-looking function
    gave me no warnings at all. Placing it as the only statement in a file
    gave me

    "Global symbol "$rule" requires explicit package name at ./foo.pl line 9 (#1)".

    Is that what you meant?
     
    Arndt Jonasson, Oct 27, 2004
    #9
  10. Arndt Jonasson <> wrote:
    >
    > Tad McClellan <> writes:
    >> Patrick Drouin <> wrote:
    >> > Although the description of my problem was not very clear, you guys
    >> > pointed me in the right direction. Here's what I used:
    >> >
    >> > $rule =~ s/\$(\w+)/${$1}/g;

    >>
    >>
    >> You should put "use strict;" in all of your Perl programs!

    >
    > Sorry for being dense, but what would "use strict" complain about in
    > the above statement?



    It uses Symbolic References.


    > Putting it inside a plausible-looking function
    > gave me no warnings at all. Placing it as the only statement in a file
    > gave me
    >
    > "Global symbol "$rule" requires explicit package name at ./foo.pl line 9 (#1)".
    >
    > Is that what you meant?



    It would be a runtime error:

    Can't use string ("foo") as a SCALAR ref while "strict refs" in use at ...

    ----------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    our $foo = 'FOO';
    my $rule = '$foo bar';
    $rule =~ s/\$(\w+)/${$1}/g;
    print "$rule\n";
    ----------------

    The code won't work with lexical (my) variables, only with
    package (our) variables.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 27, 2004
    #10
    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. Replies:
    3
    Views:
    835
    Reedick, Andrew
    Jul 1, 2008
  2. eran
    Replies:
    7
    Views:
    420
  3. Alain FELER

    regex trick

    Alain FELER, Feb 5, 2006, in forum: Ruby
    Replies:
    6
    Views:
    119
    Alain FELER
    Feb 7, 2006
  4. Kelvin
    Replies:
    2
    Views:
    113
    Anno Siegel
    Nov 9, 2004
  5. Steven D'Aprano

    readline trick needed

    Steven D'Aprano, Oct 13, 2012, in forum: Python
    Replies:
    19
    Views:
    304
    Ned Deily
    Oct 16, 2012
Loading...

Share This Page