문제

Here, I've tried to create a regular expression that matches one particular string:

#This is the string that the regex is intended to match
theString = "..!-+!)|(!+-!.."

print(re.compile(theString).match(theString))

This produces an error instead of matching the string:

raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

Is there any way to generate a regular expression that matches just one specific string, like this one?

도움이 되었습니까?

해결책

import re
your_string = "..!-+!)|(!+-!.."
your_regex_string = "^" + re.escape(your_string) + "$"
your_regex = re.compile(your_regex_string)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top