|
|
Question : search a word within string
|
|
Hi all, I want to search a character or word within a string how can i do that? e.g.
I have a string like 'goldencentre' r 'golden centre' and i want to determine whether this string contains golden r not?
Thanx wasif
|
Answer : search a word within string
|
|
Right you have two choices: in Perl Regular expression syntax or using the index subroutine.
The readable syntax is:
Regular expressions are faster and you can use various switches to make your search more detailed. Here is a extra reference/resource on them http://www.tjhsst.edu/~dhyatt/perl/exA.html
my $var = "goldencentre"; my $pattern = 'golden';
if ( $var =~ $pattern ){ #do something }
#or translated from ahoffman above #if your data is in the standard global variable # (added /i for case insensitive) # m before slash is not needed if you use standard #delimiters '/'
$_ = 'goldencentre';
if (/golden/i) {
}
or as an alternate
if (index('golden',$var) > -1 ){ #index tells you startin reference ot array/char where golden is found 'asdfsdgolden' returns 6 } if your data is same as above if (index($pattern) > -1){ }
|
|
|
|