That's what the index function is for. As long as you are comparing fixed strings then using regular expressions is overkill. Stick with index.
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
I think you're looking for index. From perldoc -f index:
index STR,SUBSTR,POSITION
index STR,SUBSTR
The index function searches for one string within another, but
without the wildcard-like behavior of a full regular-expression
pattern match. It returns the position of the first occurrence
of SUBSTR in STR at or after POSITION. If POSITION is omitted,
starts searching from the beginning of the string. The return
value is based at 0 (or whatever you'e set the $[ variable
to--but don't do that). If the substring is not found, returns
one less than the base, ordinarily "-1".
Igor 'izut' Sutton
your code, your rules.
| [reply] [d/l] [select] |
To add to the next comments, if you want to see what string is longer, use length length $st1 so you can check the longest one, as i understand you need that
#so you get something like this:
my $index = 0;
if ( lenght $str1 < length $str2 ) {
$index= index $str2,str1;
} else {
$index= index $str1,str2;
}
"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
| [reply] [d/l] [select] |
perl -e '$s1="foofoo";$s2="foo";(length$s1<length$s2)?$check=index$s2,$s1:$check=index$s1,$s2;print "match\n" if $check>=0'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@
| [reply] [d/l] |
$s1 =~ /\Q$s2/
Updated: oh, if you don't know which is shorter:
length($s1)>length($s2) ? $s1 =~ /\Q$s2/ : $s2 =~ /\Q$s1/; | [reply] [d/l] [select] |
In terms of regular expression, here is one way. Here i assume $s1 as longer string. If $s2 is longer, change the below code as vice versa.
my $s1 = 'foofoo';
my $s2 = 'foo';
print "substring" if ($s1 =~ /\Q$s2\E/); #if $s1 is longer
print "substring" if ($s2 =~ /\Q$s1\E/); #if $s2 is longer
updated: Eventhough regex is one way, as davorg suggested index is the best solution for this.
| [reply] [d/l] |