简体   繁体   中英

Regular expression OR in python findall

I am trying to use the regualr expression

re.findall("void (D|S)TC_.+\(\)", testCaseFile)

My expectation is the above expression returns list

void DTC_Sample_01()
void STC_Sample_02()

But it should ignore

void ZTC_Sample_03() or any other.

But it is not working as expected

The parenthesis you are using tells findall() to match the pattern and give you back only the contents of the parenthesis. Using ?: you are matching the pattern as previously, but instead you get the whole match.

re.findall("void (?:D|S)TC_.+\(\)", testCaseFile)

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