How to display all words that contain these characters?

后端 未结 7 1881
后悔当初
后悔当初 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:45

    >>> import re
    >>> pattern = re.compile('\b(\w*z\w*x\w*|\w*x\w*z\w*)\b')
    >>> document = '''Here is some data that needs
    ... to be searched for words that contain both z
    ... and x.  Blah xz zx blah jal akle asdke asdxskz
    ... zlkxlk blah bleh foo bar'''
    >>> print pattern.findall(document)
    ['xz', 'zx', 'asdxskz', 'zlkxlk']
    

提交回复
热议问题