regex trick needed

regex trick needed

am 26.10.2004 20:52:57 von Patrick Drouin

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

Re: regex trick needed

am 26.10.2004 21:59:28 von Paul Lalli

"Patrick Drouin" wrote in message
news:d2xfd.111835$0f.40144@charlie.risq.qc.ca...

> 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

Re: regex trick needed

am 26.10.2004 22:04:50 von Matija Papec

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

Re: regex trick needed

am 26.10.2004 22:12:26 von Gunnar Hjalmarsson

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

Re: regex trick needed

am 26.10.2004 23:10:57 von Patrick Drouin

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

Re: regex trick needed

am 26.10.2004 23:40:58 von Ben Morrow

Quoth patrick.drouin@umontreal.ca:
> 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.me.uk

Re: regex trick needed

am 26.10.2004 23:52:35 von Gunnar Hjalmarsson

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

Re: regex trick needed

am 27.10.2004 01:12:17 von Tad McClellan

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
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Re: regex trick needed

am 27.10.2004 11:03:09 von Arndt Jonasson

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?

Re: regex trick needed

am 27.10.2004 14:07:12 von Tad McClellan

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
tadmc@augustmail.com Perl programming
Fort Worth, Texas