[英]Checking if dictionary is empty using conditional statement in python is throwing a key error
[英]regex for checking conditional statement python
我正在嘗試編寫一個正則表達式,它將查找代碼(以字符串形式)中是否存在條件語句。 我嘗試了以下代碼但沒有運氣,也嘗試了一些相關問題。
將被檢查的代碼:
if 條件可能有兩種類型
一種是:
if True:
print_content((weather_info('London')))
另一種是:
if (weather_info('London')):
print('success')
我也試過一些問題,它們如下:
檢查條件正則表達式的代碼:
if re.match(r'^if([A-Z][a-z]:)', code):
self.conditional_step+=1
print("condtional step:"+str(self.conditional_step))
謝謝大家的配合。
我確實通過制作三個不同的正則表達式來完成我的工作。我在下面添加這些正則表達式,希望這可能對其他人有幫助,
regex_if_true = re.compile(r"^if\strue:|^if\sTrue:$", re.MULTILINE)
regex_if_false = re.compile(r"^if\sfalse|^if\sFalse:$", re.MULTILINE)
regex_service = re.compile(r"(if).*?:$", re.MULTILINE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.