Python:如果带有动态变量的字符串

时间:2017-10-29 00:12:02

标签: python

好的,这可能是错误的措辞,如果是的话,我会爱一些人来纠正我。我试图找出一个字符串是否包含某个短语,即使该短语的某些部分是动态的。

例如,字符串可以是:

  • 大家好,Jordan于10/02/19在圣托马斯注册
  • 您好,Lisa已于16/11/19在圣托马斯注册
  • 您好,Craig已于12月10日在Sirius Academy注册

在我的python中我有这个:

    if "Hi there, {$0} has enrolled at {$1} on ${2}" in email_body:
           print("Someone new is arriving...")

然而它不会发射。如果我打印email_body它会向我显示电子邮件,所以问题在于if语句和正则表达式检测。

奇怪的结果编辑:

这是我的代码:

data = re.findall('Hi there, (.*?) has enrolled at (.*?) on (.*?)', message_body)[0]
print(data)

返回:

  

('Lisa','St。Thomas','')

由于某种原因,缺少第三个值。

当我打印(email_body)时,我得到了:

  

您好,Lisa已于2017年9月16日入读圣托马斯

2 个答案:

答案 0 :(得分:3)

您可以使用re.findall

import re
email_body = 'Hi there, Lisa has enrolled at St. Thomas on 16/11/19'
if re.findall('Hi there, [\w\W]+ has enrolled at [\w\W]+ on [\w\W]+', email_body):
    print("Someone new is arriving...")

关于你最近的评论,如果你想要整行,你可以这样做:

email_body = 'Hi there, Lisa has enrolled at St. Thomas on 16/11/19'
data = re.findall('Hi there, [\w\W]+ has enrolled at [\w\W]+ on [\w\W]+', email_body)
if data:
    print(data[0])

输出:

'Hi there, Lisa has enrolled at St. Thomas on 16/11/19'

新编辑:更复杂的字符串

email_body1 = '53ewwffHi there, Lisa has enrolled at St. Thomas on 16/11/19\n \n dfdsg 45435'
email_body2 = "Hi there, Lisa has enrolled at St. Thomas on 16thSept2017"
data = re.findall('Hi there, (.*?) has enrolled at (.*?) on ([a-zA-Z0-9/]+)', email_body1)
data1 = re.findall('Hi there, (.*?) has enrolled at (.*?) on ([a-zA-Z0-9/]+)', email_body2)
print(data[0])
print(data1[0])

输出:

('Lisa', 'St. Thomas', '16/11/19')

('Lisa', 'St. Thomas', '16thSept2017')

答案 1 :(得分:1)

你是正确的,你想在这里使用正则表达式。例如:

>>> import re
>>> r = re.match(r'Hi there, (.+) has enrolled at (.+) on (.+)', 'Hi there, Jordan has enrolled at St. Thomas on 10/02/19')
>>> r.groups()
('Jordan', 'St. Thomas', '10/02/19')

使用它们:

>>> person, place, day = r.groups()
>>> '{} / {} / {}'.format(person, place, day)
'Jordan / St. Thomas / 10/02/19'