RegExp - compare two strings

Posted on 2005-05-17
Last Modified: 2007-12-19
I need a regexp. that compares two strings and checks if they have at least one word in common.

string1 = "The man has a dog"
string2 = "I don't like cats but I have a dog"

Both strings contains the words "a" and "dog" and the expression should now return true.
I imagine that it is possible to write one expression that handles this instead of doing pattern matching on every word from one string in the other string.
Question by:fedtmule
    LVL 53

    Expert Comment

    You could go over every word in string1 and check with a regular expression whether string2 contains it too. Otherwise i don't really understand your question.
    LVL 96

    Expert Comment

    by:Bob Learned
    AFAIK, this would be virtually impossible to do with one expression, since the strings are in 2 separate variables.  The idea of breaking the string into words, storing each word in a hash table, and then searching by key, and incrementing value would be my solution for the problem.

    LVL 2

    Expert Comment

    Previous solution is the usual clean fast way to do it

    If you really want a regexp you can try to match the pattern "( The | man | has | a | dog )" against space+string2+space

    python oneliner;
    if re.compile("("+"|".join(map(lambda w:" "+re.escape(w)+" ",string1.split(" ")))+")").search(" "+string2+" "):

    (but really something this unreadable should be written in perl)
    (please give points to the guy above !)

    Regular expressions are meant to search for patterns not words

    Author Comment

    How about something like the this (I'm using c#):

    string str1 = "The man has a dog"
    string str2 = "I don't like cats but I have a dog"

    string sepseq = " XXXXXX ";
    string Pattern = @"\b([a-z]+?)\b.*" + sepseq + @".*\b\1\b";
    string useText = str1 + sepSeq + str2;            

    Regex RE = new Regex(Pattern, RegexOptions.IgnoreCase);
    if (RE.IsMatch(useText))
        Console.WriteLine("At least one word appears in both texts");
        Console.WriteLine("The two texts has no words in common");

    Author Comment

    Any comments on my last post?
    LVL 2

    Accepted Solution

    Closed, 250 points refunded.
    Friendly Neighbourhood Community Support Admin

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    canBalance challenge 34 56
    linearIn  challenge 23 54
    Problem to setup 18 67
    allStar challenge 1 27
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    731 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

    16 Experts available now in Live!

    Get 1:1 Help Now