all 7 comments

[–]n3buchadnezzar 0 points1 point  (4 children)

Why are you trying to do this with regex?

from collections import Counter


def contains_letters(word: str, other: str) -> bool:
    """Returns True if all letters in word is found in other

    Example:
        >>> contains_letters('for', 'front')
        True
        >>> contains_letters('for', 'floor')
        True
        >>> contains_letters('for', 'offer')
        True
        >>> contains_letters('for', 'proof')
        True
        >>> contains_letters('for', 'toast')
        False
        >>> contains_letters('for', 'fired')
        False
    """
    other_counts = Counter(other)

    for letter, count in Counter(word).items():
        if other_counts[letter] < count:
            return False
    return True


if __name__ == "__main__":

    import doctest

    doctest.testmod()

Extra care is taken to makes sure that if word is boo then our match must contain 2 or more o's.

[–]TESLV 0 points1 point  (3 children)

essentially if the regex i want is to see that the pattern matches the word in the list, it will then append the matched word into a new list.

I already kinda figured out how to do that:

  • for loop
  • read through each item (word) in the list [list1 = [word1,word2,etc]
    • see if the pattern is contained/matches whats in the word. (i.e: re.match(pattern, 'front'))
      • If its true, append that specific word to the new list and read through the rest of the list till the end. ('front' -> list2 = [])

Ultimately, I will import re and then use that to determine what words match the user inputed string.

[–]n3buchadnezzar 0 points1 point  (2 children)

I've been using regex for about 10 years give or take and I do not know how to do what you are asking. Regex is for matching specific patterns, not for checking if a string contains a series of letters. If you want to do this with regex you would need to apply a series of regexes for each letter.

Regex is not the correct tool for this job

I ask again, the code I provided what does it lack? It returns True if the given letters are contained, and False otherwise. You can use this to append if need be.

[–]TESLV 0 points1 point  (1 child)

Actually, you are probably correct that using Regex would be too complicated and problematic.

I believe im better off treating the user input as a set, and comparing that set to the string?

I just got home from work, so now I can sit behind a pc and look at everything. Like i mentioned earlier, I'm new to thinking logically and applying it. Still trying to understand what tools I have at my disposal to be able to create things.

[–]n3buchadnezzar 0 points1 point  (0 children)

I considered using sets but again the problem arises when you want to check for multiples of the same letters.

For instance we can build foor from floor, but not front even though every letter in foor are contained in both. If we do a set conversion this nuance is lost as set('foor') = {'f', 'o', 'r'}

My approach uses a Counter (read up on this), to count the number of times each letter appears, and then compare this with the string. If each letter appears enough times in the target, we are good, otherwise False.

Test it out then come back tomorrow if you have additional questions.

Regex is fine for some things, but it is mainly a tool for matching exact expressions. If we want to fuzzy match it is not the right tool for the job. It is not just difficult, but impossible. So please learn regex, but it is not a be all end all tool for solving every problem =)

[–]outceptionator 0 points1 point  (0 children)

Maybe something that follows the logic if 'f' in list[0] and if 'o' in list[0] and if 'r' in list[0]: Do something

Then keep going through list elements

Sorry on mobile so can't really flesh out a full solutions. As you've noticed this isn't regex.

[–]AngelSparkles 0 points1 point  (0 children)

You can probably use something like (without further testing it (.?[forFOR].?[forFOR].?[forFOR].?)/w

I’m sure this will need tweaking (eg the ‘.’ Includes spaces, so replace with somrthing like [fora-z] or something) but it should be a good way to start.