String Compare in Perl? (Most likely reg expression?)

Posted on 2011-04-23
Medium Priority
Last Modified: 2012-05-11
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.

Question by:KGNickl
LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 35454645
print "girl, boy2b, cat" =~ /\bboy\b/?"true":"false";
print "girl, boy2b, cat" =~ /\bboy2b\b/?"true":"false";

LVL 31

Expert Comment

ID: 35454670
if( $str eq $str2)

Author Comment

ID: 35454798
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";
	print "false";

Open in new window


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question