Regular expression to find parts of a string in another

I have two lines: the first value is "catdog" and the second is "got".

I am trying to find a regular expression that tells me if the letters for "got" are in "catdog". I especially try to avoid the case when there are duplicate letters. For example, I know that “got” matches, but “gott” doesn't match, because there are no two “t” in “catdog”.

EDIT:

Based on Adam's answer below, this is the C # code that I need to work in my solution. Thanks to all who responded.

Note. I had to convert char to int and subtract 97 to get the corresponding index for the array. In my case, the letters are always lowercase.

    private bool CompareParts(string a, string b)
    {

        int[] count1 = new int[26];
        int[] count2 = new int[26];

        foreach (var item in a.ToCharArray())
            count1[(int)item - 97]++;

        foreach (var item in b.ToCharArray())
            count2[(int)item - 97]++;

        for (int i = 0; i < count1.Length; i++)
            if(count2[i] > count1[i])
                return false;

        return true;
    }
+3
7

. , . , . - , , . , , :

bool containsParts(string1, string2)
{
    count1 = array of 26 0's
    count2 = array of 26 0's

    // Note: be sure to check for an ignore non-alphabetic characters,
    // and do case conversion if you want to do it case-insensitively
    for each character c in string1:
        count1[c]++
    for each character c in string2:
        count2[c]++

    for each character c in 'a'...'z':
        if count1[c] < count2[c]:
            return false

    return true
}
+7

, , , - , , , , , , , - .

, :

MatchString.ToList().ForEach(Item => Input.Remove(Item));

:

public bool IsSubSetOf(string InputString, string MatchString) 
{
  var InputChars = InputString.ToList(); 
  MatchString.ToList().ForEach(Item => InputChars.Remove(Item)); 
  return InputChars.Count == 0;
}

, , .

, "got" , , "gott" , "t" . , . , "gott" - "catdog", "got".

:

using System;
using System.Linq;
using System.Runtime.CompilerServices;

static class extensions
{
    public static bool IsSubSetOf(this string InputString, string MatchString)
    {
        var InputChars = InputString.ToList();
        MatchString.ToList().ForEach(Item => InputChars.Remove(Item));
        return InputChars.Count == 0;
    }
}

, thins , :

Console.WriteLine("gott".IsSubSetOf("catdog"));
+3

, , . , , -

^[^got]*(g|o|t)[^got]$

" ", .

0

, . :

/^(c?a?t?d?o?g?|c?a?t?d?g?o?| ... )$/

, , ( Perl, ):

$foo = 'got';
$foo =~ s/c//;
$foo =~ s/a//;
...
$foo =~ s/d//;
# if $foo is now empty, it passes the test.

, , :

$foo = 'got'
foreach $l (split(//, 'catdog') {
    $foo =~ s/$l//;
}
# if $foo is now empty, it passes the test.

, , . , , , , Perl, .

0
, . , lookaheads , :
/^
 (?=[^got]*g[^got]*$)
 (?=[^got]*o[^got]*$)
 [^got]*t[^got]*
$/x

, , . , , , - . , .

0

@Adam Rosenfield Python:

from collections import defaultdict

def count(iterable):
    c = defaultdict(int)
    for hashable in iterable:
        c[hashable] += 1
    return c

def can_spell(word, astring):
    """Whether `word` can be spelled using `astring` characters."""

    count_string = count(astring)
    count_word   = count(word)

    return all(count_string[c] >= count_word[c] for c in word)
0

- IMO:

. ( )    : "catdog" "acdgot"

.

  • Do the same with the line you are looking for characters: "gott" becomes, eh, "gott" ...

  • Insert a " .*" between each of these characters

  • Use the latter as a regular expression to search in the former.

For example, some Perl code (if you don't mind):

$main = "catdog"; $search = "gott";
# break into individual characters, sort, and reconcatenate
$main = join '', sort split //, $main;
$regexp = join ".*", sort split //, $search;
print "Debug info: search in '$main' for /$regexp/ \n";
if($main =~ /$regexp/) {
    print "Found a match!\n";
} else {
    print "Sorry, no match...\n";
}

Fingerprints:

Debug info: search in 'acdgot' for /g.*o.*t.*t/
Sorry, no match...

Drop one “t” and you get a match.

0
source

All Articles