*Really* Simple Regular Expressions...

Status
Not open for further replies.
I have a problem, and while I'm sure that standard regular expressions would work, they seem extremely overkill for the situation.

These threads didn't really help.

I have a collection of substrings Si, and a collection of target strings Ti. I want to compare the list of substrings to each target string, according to the following condition: The substrings must all be present in the target, in order, but they may have any number of characters between them.

Example:
S1 = "i"
S2 = "ow"
S3 = "ped"

T1 = "the quick brown fox jumped over the lazy dog"
T2 = "the rain in spain stays mainly in the plain"

In this example, TI would succeed and T2 would fail.

The comparison basically breaks down to evaluating the regular expression "*i*ow*ped*" on each target string, but this seems wasteful for such a simple problem.

My other idea is to keep lists of which targets pass each substring test, and iterate through Si until either the latest list has no entries (all the targets failed) or it runs out of substrings (one or more targets passed). This takes advantage of two things I know about the situation:

1. The number of substrings will be low (<25 likely)
2. The number of targets could be quite high

Please lend a hand if you see a better way!
 

Drommesyn

Smack-Fu Master, in training
87
If SubStringPos() in the pseudocode above behaves like the C standard library function strstr(), then this solution would declare a false negative result for S1 = 'i', S2 = 'ow', S3 = 'ped', T = 'pediowped', because S3 would be matched at position zero, ahead of the match for S2.

I believe that you can fix it easily enough. Something like this (which I haven't tested, or even compiled...)

Code:
bool match_substrings(const char* p_search_string, const char** p_substring_to_match, unsigned int n_substrings)
{
    // assert(p_search_string != 0 && p_substring_to_match[i] != 0 for each i...
    
    const char* p_last_match_start = p_search_string;
    const char* p_last_match_end   = p_search_string;
    
    for (unsigned int i = 0; i < n_substrings; ++i)
    {
        const char* p_match = strstr(p_last_match_end, p_substring_to_match[i]);
    	
        if (p_match == 0)
        {
            return false;
        }
        else
        {
            p_last_match_start = p_match;
            p_last_match_end   = p_last_match_start + strlen(p_substring_to_match[i]);
        }
    }
    
    return true;
}
 
If SubStringPos() in the pseudocode above behaves like the C standard library function strstr(), then this solution would declare a false negative result for S1 = 'i', S2 = 'ow', S3 = 'ped', T = 'pediowped', because S3 would be matched at position zero, ahead of the match for S2.
That's what I want :D

Basically, the user will enter in a series of substrings in order (most one letter long but longer if needed) and the program will search for all target strings that match.

The main goal is to let people type abreviations to find their stuff. If I wanted to find Microsoft Word, I'd type M W or MIC W D or whatever, and that would narrow down the search to the point where I can use the arrow keys to navigate the results. By default each character is considered a separate substring, unless the shift key is being held while they are entered; then they are grouped together. After each new substring is entered, the target strings which no longer pass "fall back" like the losing contestants on Singled Out until there are a managable number of items to choose from.
 

Drommesyn

Smack-Fu Master, in training
87
Is that really what you want? To consider a real-life example - in fact, your real-life example:

Suppose that you're trying to find strings matching 'Microsoft' and 'Word' in that order. The original code would return a negative result for the string 'Keywords: Microsoft Word.' It would match 'Microsoft' at position 10 (starting from zero) and 'word' at position 3, and on that basis report a failure because the matches are not in the correct order. The 'word' starting at index 20 never comes into play.
 
Status
Not open for further replies.