如何仅在 f1 匹配时打印 else
How to print else only IF f1 is a match
以下正则表达式目前运行良好。它将 return IF f1 和 f2 或 f3 匹配。
我需要帮助弄清楚如何仅在 f1 匹配时打印 else,例如print("哪个城市,哪个月份?)
import re
string = ['I am looking to rent a home in Los Angeles']
for s in string:
f1 = re.findall(r'(.{0,50}\b(home|house|condo)\b.{0,50})',s, re.IGNORECASE)
if f1:
f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f2:
print("For what month?")
f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f3:
print("For what city?")```
确定 f1
匹配后,收集 f2
和 f3
匹配。然后检查 f2
或 f3
是否匹配,否则,执行 f1
block:
import re
string = ['I am looking to rent a home in Los Angeles']
for s in string:
f1 = re.search(r'.{0,50}\b(home|house|condo)\b.{0,50}',s, re.IGNORECASE)
if f1:
f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})', f1.group(), re.IGNORECASE)
f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})', f1.group(), re.IGNORECASE)
if f2:
print("For what month?")
elif f3:
print("For what city?")
else:
print("Only f1 fired, not f2 and f3")
else:
print("No f1!")
以下正则表达式目前运行良好。它将 return IF f1 和 f2 或 f3 匹配。
我需要帮助弄清楚如何仅在 f1 匹配时打印 else,例如print("哪个城市,哪个月份?)
import re
string = ['I am looking to rent a home in Los Angeles']
for s in string:
f1 = re.findall(r'(.{0,50}\b(home|house|condo)\b.{0,50})',s, re.IGNORECASE)
if f1:
f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f2:
print("For what month?")
f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})',f1[0][0], re.IGNORECASE)
if f3:
print("For what city?")```
确定 f1
匹配后,收集 f2
和 f3
匹配。然后检查 f2
或 f3
是否匹配,否则,执行 f1
block:
import re
string = ['I am looking to rent a home in Los Angeles']
for s in string:
f1 = re.search(r'.{0,50}\b(home|house|condo)\b.{0,50}',s, re.IGNORECASE)
if f1:
f2 = re.findall(r'(.{0,50}\b(los angeles|la)\b.{0,50})', f1.group(), re.IGNORECASE)
f3 = re.findall(r'(.{0,50}\b(june|july|august)\b.{0,50})', f1.group(), re.IGNORECASE)
if f2:
print("For what month?")
elif f3:
print("For what city?")
else:
print("Only f1 fired, not f2 and f3")
else:
print("No f1!")