Matching characters in two Python lines

I am trying to print common characters between two sets of strings in Python, I do this with the hope that I actually find how to do it using python regex only (I don't know regex so this might be a good time to study it).

So if first_word = "peepa"

and second_word = "poopa"

, I want the return value to be: "pa"

since in both variables the characters that are common are p and a. So far I have been following the documentation on how to use the re module, but I cannot understand the basic concepts of this.

Any ideas as to how I can resolve this issue?

+3


source to share


3 answers


This is similar to a problem when you want to find the intersection of characters between two strings. The fastest way is to do it:

>>> set(first_word).intersection(second_word)
set(['a', 'p'])

      



I don't think regex is the right fit for this problem.

+8


source


Use kits. Casting a string to a set returns an iterable with unique letters. Then you can get the intersection of the two sets.



match = set(first_word.lower()) & set(second_word.lower())

      

+4


source


Using regular expressions

This problem is specifically designed for sets. But you are asking "how to do this using python regexes only".

Here's a start:

>>> import re
>>> re.sub('[^peepa]', '', "poopa")
'ppa'

      

The above example uses regular expressions to remove from poopa every letter that has not yet been in peepa. (As you can see, this leaves duplicate letters that don't get executed.)

re.sub

Performs substitutions based on regular expressions in more detail . [peepa]

is a regular expression that means any of the letters peepa

. Regular expression [^peepa]

means anything that is not in peepa

. Anything that matches this regular expression is replaced with an empty string ""

, i.e. Removed. Only common letters remain.

+1


source







All Articles