Click here to get back home

Regex newbie

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Regex newbie gazza67 03-10-2008
`--> Re: Regex newbie Peter Ludikovsk...03-11-2008
Posted by gazza67 on March 10, 2008, 11:30 pm
Please log in for more thread options
Anybody,

I want to use a regex in which the string for which we are searching
is a variable. Is this possible - it doesnt seem to work for me.

$a = "be";

$b="to be or not to be";
if ($b =~ m/$a/i )
print "this should be true";

$b = "apples and oranges";
if ($b =~ m/$a/i )
print "this should be false";



Cheers
Gary

Posted by kath on March 11, 2008, 12:45 am
Please log in for more thread options
> Anybody,
>
> I want to use a regex in which the string for which we are searching
> is a variable. Is this possible - it doesnt seem to work for me.
>
> $a = "be";
>
> $b="to be or not to be";
> if ($b =~ m/$a/i )
> print "this should be true";
>
> $b = "apples and oranges";
> if ($b =~ m/$a/i )
> print "this should be false";
>
> Cheers
> Gary

Either you have to use post condition or have to close block after if
statement with brackets

print "this should be true" if ($b =~ /$a/i);

or

if ($b =~ /$a/i){
print "this should be true";
}

sudhir.

Posted by Peter Ludikovsky on March 11, 2008, 4:14 am
Please log in for more thread options
gazza67 wrote:
> Anybody,
>
> I want to use a regex in which the string for which we are searching
> is a variable. Is this possible - it doesnt seem to work for me.
>
> $a = "be";
>
> $b="to be or not to be";
> if ($b =~ m/$a/i )
> print "this should be true";
>
> $b = "apples and oranges";
> if ($b =~ m/$a/i )
> print "this should be false";
>
>
>
> Cheers
> Gary

1) Don't use $a & $b. Quote from 'perldoc perlvar':
        Special package variables when using sort(), see "sort" in
        perlfunc. Because of this specialness $a and $b don't need to
        be declared (using use vars, or our()) even when using the
        "strict 'vars'" pragma. Don't lexicalize them with "my $a" or
        "my $b" if you want to be able to use them in the sort() com‐
        parison block or function.

2) You can make a better distinction between matching text and matching
variables by enclosing your variables in curly brackets, eg:
        $string =~ /$/;

HTH
/peter

Similar ThreadsPosted
Newbie question on regex. October 27, 2004, 6:31 pm
regex newbie question March 28, 2005, 2:47 am
[newbie] How can I catch a C comment with a regex? October 8, 2004, 10:30 am
Printing only a portion of a matched regex -- newbie quesiton August 9, 2004, 1:49 pm
regex bug (comments within regex not as robust) October 27, 2005, 12:01 pm
CGI newbie July 16, 2004, 9:04 am
Newbie needs help... July 28, 2004, 3:58 pm
Help for newbie March 23, 2005, 6:41 pm
Newbie Help April 26, 2005, 10:41 pm
Newbie Help June 23, 2005, 11:34 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap