python字符串中的正则表达式

时间:2017-11-21 10:54:48

标签: python regex

我想在文本文件中找到符合某些单词顺序的行。 例如:

customer is given as per below
customer are given not priority below
given given below customer
below customer information given

我试过这个(?=.*customer)(?=.*given)(?=.*below),但是工作。

它应该只给出

customer is given as per below 
customer are given not priority below

我希望订单相同: “顾客”在第一名,“给予”在第三名,“下”应该在第六名。

1 个答案:

答案 0 :(得分:-1)

这有效:

import re

s = '''customer is given as per below
customer are given not priority below
given given below customer
below customer information given
customer is given not as per below'''

matches = re.findall('customer \S+ given \S+\s\S+ below', s)
for match in matches:
    print(match)