Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple regular expression question

Status
Not open for further replies.

SaltyDuke

Programmer
Sep 18, 2002
140
IE
hello

i'm having a little trouble getting my regular expression to work properly. Basically, i just want to search thru a line of text to see if it matches the contents of a variable, like so:

if($tring =~ /$compare/)
{
print "whoppee!!";
}

only there are 2 problems:
1. either string may be either lowercase or uppercase.
2. $compare may occur anywhere in $tring.

so i just need 3 pieces of info: an upper/lowercase function, an inexact pattern matching expression (is that m// ???) and if i'm using the if statement above correct.

(as yo can tell, my knowledge of regular expressions/pattern matching is patchy to say the least. nyone know a good tutorial?? :))

Thanx in advance
TheSaltyDuke

[pipe]
 
Hi,
if($string =~/$compare/i)
{
print "whoppee!!";
}

i = ignore case.

$compare may occur anywere in $string, this expression will retrun true. That's because you regexp is not anchored.

$string=~/^$compare/i will match only if $string start with $compare.

The link below is to an official perl regular expressions tutorial :


Regards,
Zephan
 
Looks like you've got it right - just missing one thing

if($tring =~ /$compare/i)
{
print "whoppee!!";
}

i will tell the regex to ignore case

The above should work with no problem

Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top