How to display all words that contain these characters?

后端 未结 7 1877
后悔当初
后悔当初 2021-01-03 00:05

I have a text file and I want to display all words that contains both z and x characters.

How can I do that ?

7条回答
  •  春和景丽
    2021-01-03 00:44

    Assuming you have the entire file as one large string in memory, and that the definition of a word is "a contiguous sequence of letters", then you could do something like this:

    import re
    for word in re.findall(r"\w+", mystring):
        if 'x' in word and 'z' in word:
            print word
    

提交回复
热议问题