简体   繁体   English

用于检查条件语句python的正则表达式

[英]regex for checking conditional statement python

I am trying to write a regex that will find whether there is a condition statement into a code (which is in the form of string) or not.我正在尝试编写一个正则表达式,它将查找代码(以字符串形式)中是否存在条件语句。 I have tried the following code but no luck and tried some relevant questions too.我尝试了以下代码但没有运气,也尝试了一些相关问题。

Code which will be checked:将被检查的代码:

There might be two types of if condition if 条件可能有两种类型

One is:一种是:

if True:
    print_content((weather_info('London')))

Another one is:另一种是:

if (weather_info('London')):
    print('success')

I have also tried some questions and they are as follows:我也试过一些问题,它们如下:

python regex for conditional (|) matching 用于条件(|)匹配的python正则表达式

Code for checking the conditional regex:检查条件正则表达式的代码:

if re.match(r'^if([A-Z][a-z]:)', code):
    self.conditional_step+=1
    print("condtional step:"+str(self.conditional_step))

Thanks for everyone's co-operation.谢谢大家的配合。

I do get my job done by making three different regex.And i am adding those regex in the following and hope this might be helpful for someone else,我确实通过制作三个不同的正则表达式来完成我的工作。我在下面添加这些正则表达式,希望这可能对其他人有帮助,

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM