re.search(r'data check\s*:\s*false\((\d+)\)', content).group(1) if re.search(r'data check\s*:\s*false\((\d+)\)', content) else "NA"
how to this regex in similar and faster way ? i have so many regex to check how to do it in efficient way ?
re.search(r'data check\s*:\s*false\((\d+)\)', content).group(1) if re.search(r'data check\s*:\s*false\((\d+)\)', content) else "NA"
how to this regex in similar and faster way ? i have so many regex to check how to do it in efficient way ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
sasikala sPosted May 28, 2025, 4:27 AM
match = re.search(r'data check\s*:\s*false\((\d+)\)', content)
result = match.group(1) if match else "NA"
re.search(...)looks for the patterndata check : false() in thecontentstring.group(1)extracts the number inside the parentheses."NA".Amira BedhiafiPosted May 27, 2025, 7:11 PM
You're calling
re.search()twice, once to check if there's a match, and again to get.group(1). Instead, store the match in a variable:If you're running the same regex many times (in a loop or on multiple lines), compile it once:
If you're matching multiple regexes on the same input and each has a similar pattern :