简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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