KGNickl
asked on
String Compare in Perl? (Most likely reg expression?)
I need to write a perl if statement that compares two strings and if string one is equal to part of string 2 then it return true, otherwise it returns false.
The first string will be a single value such as hello. It could be values such as hello, hello1a, person, dog, etc... to give you an idea.
The second string will be multiple words separated by commas and ending in which space such as: girl, boy2b, cat, hello1a
I need to write code where if string 1 is contained in string 2 (from comma to comma or comma to whitespace) I return true, otherwise I return false. The main thing to consider is the last word will not be followed by a comma but rather nothing and all other word will be separated by a comma.
For example if the word is boy and string to compare is: girl, boy2b, cat then it would return false.
For example if the word is boy2b and string to compare is: girl, boy2b, cat then it would return true.
The thing that makes this more difficult for me is the last word not being separated by a comma and the fact that a word has to be exact. So boy1a is not the same as boy.
Thanks!
The first string will be a single value such as hello. It could be values such as hello, hello1a, person, dog, etc... to give you an idea.
The second string will be multiple words separated by commas and ending in which space such as: girl, boy2b, cat, hello1a
I need to write code where if string 1 is contained in string 2 (from comma to comma or comma to whitespace) I return true, otherwise I return false. The main thing to consider is the last word will not be followed by a comma but rather nothing and all other word will be separated by a comma.
For example if the word is boy and string to compare is: girl, boy2b, cat then it would return false.
For example if the word is boy2b and string to compare is: girl, boy2b, cat then it would return true.
The thing that makes this more difficult for me is the last word not being separated by a comma and the fact that a word has to be exact. So boy1a is not the same as boy.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if( $str eq $str2)
ASKER
Took Ozo's answer and rewrote it long form. Works great! Thanks! Saved me 30 minutes or more of me searching around and playing with regular expressions hoping to get something to work.
my $value1 = "girl, boy2b, cat";
my $value2 = "boy2b";
#my $value2 = "boy";
if($value1 =~ /\b$value2\b/){
print "true";
}else{
print "false";
}