I have two strings: the first’s value is ‘catdog’ and the second’s is ‘got’.
I’m trying to find a regex that tells me if the letters for ‘got’ are in ‘catdog’. I’m particularly looking to avoid the case where there are duplicate letters. For example, I know ‘got’ is a match, however ‘gott’ is not a match because there are not two ‘t’ in ‘catdog’.
EDIT:
Based on Adam’s response below this is the C# code I got to work in my solution. Thanks to all those that responded.
Note: I had to convert the char to int and subtract 97 to get the appropriate index for the array. In my case the letters are always lower case.
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; }
You’re using the wrong tool for the job. This is not something regular expressions are capable of handling easily. Fortunately, it’s relatively easy to do this without regular expressions. You just count up the number of occurrences of each letter within both strings, and compare the counts between the two strings – if for each letter of the alphabet, the count in the first string is at least as large as the count in the second string, then your criteria are satisfied. Since you didn’t specify a language, here’s an answer in pseudocode that should be easily translatable into your language: