Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex Check if two strings share two common characters

I'm just getting to know regular expressions, but after doing quite a bit of reading (and learning quite a lot), I still have not been able to figure out a good solution to this problem.

Let me be clear, I understand that this particular problem might be better solved not using regular expressions, but for the sake of brevity let me just say that I need to use regular expressions (trust me, I know there are better ways to solve this).

Here's the problem. I'm given a big file, each line of which is exactly 4 characters long.

This is a regex that defines "valid" lines:

"/^[AB][CD][EF][GH]$/m" 

In english, each line has either A or B at position 0, either C or D at position 1, either E or F at position 2, and either G or H at position 3. I can assume that each line will be exactly 4 characters long.

What I'm trying to do is given one of those lines, match all other lines that contain 2 or more common characters.

The below example assumes the following:

  1. $line is always a valid format
  2. BigFileOfLines.txt contains only valid lines

Example:

// Matches all other lines in string that share 2 or more characters in common
// with "$line"
function findMatchingLines($line, $subject) {
    $regex = "magic regex I'm looking for here";
    $matchingLines = array();
    preg_match_all($regex, $subject, $matchingLines);
    return $matchingLines;
}

// Example Usage
$fileContents = file_get_contents("BigFileOfLines.txt");
$matchingLines = findMatchingLines("ACFG", $fileContents);

/*
 * Desired return value (Note: this is an example set, there 
 * could be more or less than this)
 * 
 * BCEG
 * ADFG
 * BCFG
 * BDFG
*/

One way I know that will work is to have a regex like the following (the following regex would only work for "ACFG":

"/^(?:AC.{2}|.CF.|.{2}FG|A.F.|A.{2}G|.C.G)$/m"

This works alright, performance is acceptable. What bothers me about it though is that I have to generate this based off of $line, where I'd rather have it be ignorant of what the specific parameter is. Also, this solution doesn't scale terrible well if later the code is modified to match say, 3 or more characters, or if the size of each line grows from 4 to 16.

It just feels like there's something remarkably simple that I'm overlooking. Also seems like this could be a duplicate question, but none of the other questions I've looked at really seem to address this particular problem.

Thanks in advance!

Update:

It seems that the norm with Regex answers is for SO users to simply post a regular expression and say "This should work for you."

I think that's kind of a halfway answer. I really want to understand the regular expression, so if you can include in your answer a thorough (within reason) explanation of why that regular expression:

  • A. Works
  • B. Is the most efficient (I feel there are a sufficient number of assumptions that can be made about the subject string that a fair amount of optimization can be done).

Of course, if you give an answer that works, and nobody else posts the answer *with* a solution, I'll mark it as the answer :)

Update 2:

Thank you all for the great responses, a lot of helpful information, and a lot of you had valid solutions. I chose the answer I did because after running performance tests, it was the best solution, averaging equal runtimes with the other solutions.

The reasons I favor this answer:

  1. The regular expression given provides excellent scalability for longer lines
  2. The regular expression looks a lot cleaner, and is easier for mere mortals such as myself to interpret.

However, a lot of credit goes to the below answers as well for being very thorough in explaining why their solution is the best. If you've come across this question because it's something you're trying to figure out, please give them all a read, helped me tremendously.

like image 907
Paul Hazen Avatar asked Apr 22 '12 22:04

Paul Hazen


1 Answers

Why don't you just use this regex $regex = "/.*[$line].*[$line].*/m";?

For your example, that translates to $regex = "/.*[ACFG].*[ACFG].*/m";

like image 71
Jack Avatar answered Oct 13 '22 00:10

Jack