?
Solved

RegExp - compare two strings

Posted on 2005-05-17
7
Medium Priority
?
1,406 Views
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.
0
Comment
Question by:fedtmule
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 14018698
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14018929
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.

Bob
0
 
LVL 2

Expert Comment

by:alain_tesio
ID: 14020388
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:fedtmule
ID: 14025017
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");
else
    Console.WriteLine("The two texts has no words in common");
   
   
0
 

Author Comment

by:fedtmule
ID: 14067046
Any comments on my last post?
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 14147189
Closed, 250 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Make the most of your online learning experience.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…
Loops Section Overview

621 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