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

Posted on 2011-04-23
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 84

    Accepted Solution

    print "girl, boy2b, cat" =~ /\bboy\b/?"true":"false";
    print "girl, boy2b, cat" =~ /\bboy2b\b/?"true":"false";

    LVL 31

    Expert Comment

    if( $str eq $str2)
    LVL 5

    Author Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Title # Comments Views Activity
    sameEnds challenge 25 55
    withoutTen challenge 14 68
    Regular expression that only finds last instance 1 34
    triangle challenge 4 34
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now