Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

* (asterik) in regexp

1 view
Skip to first unread message

monkeys paw

unread,
Feb 1, 2010, 10:16:49 PM2/1/10
to
If i have

if ($item =~ /$exp/) {
print 'yes';
}

My problem is that $item has a '*' in it at times.
I'm getting weird behavior so i'm asking.

Is this correct?

$exp =~ s/\*/\\\*/g;
if ($item =~ /$exp/) {
print 'yes';
}

J�rgen Exner

unread,
Feb 1, 2010, 10:23:45 PM2/1/10
to
monkeys paw <us...@example.net> wrote:
>If i have
>
>if ($item =~ /$exp/) {
> print 'yes';
>}
>
>My problem is that $item has a '*' in it at times.
>I'm getting weird behavior so i'm asking.
>
>Is this correct?

Maybe. Depends on what that code snippet of yours is supposed to do. And
you forgot to tell us what it is supposed to do.

>$exp =~ s/\*/\\\*/g;
>if ($item =~ /$exp/) {
> print 'yes';
>}

jue

Martijn Lievaart

unread,
Feb 2, 2010, 3:18:35 AM2/2/10
to

You are probably looking for \Q...\E, see perldoc perlre.

HTH,
M4

RedGrittyBrick

unread,
Feb 2, 2010, 4:46:35 AM2/2/10
to

Could you be clearer about your question by posting a runnable example
that illustrates your problem?

------------------------8<----------------------
#!perl
use strict;
use warnings;

my $re = ' \* ';

while (<DATA>) {
chomp;
print;
print /$re/ ? " Matches\n" : " Doesn't\n";
}

__DATA__
lorem * ipsum
dolor sit amet
adispic*ing
------------------------8<----------------------
lorem * ipsum Matches
dolor sit amet Doesn't
adispic*ing Doesn't

0 new messages