I am writing an online regex checker, which takes input from the user in the form of a pattern, and flags, and uses that to compile a regex object. The regex object is then used to check if the test string matches within the format provided by the regex pattern or not. As of this moment, the compile function looks like this:
class RegexObject:
...
def compile(self):
flags = ''
if self.multiline_flag:
flags = re.M
if self.dotall_flag:
flags |= re.S
if self.verbose_flag:
flags |= re.X
if self.ignorecase_flag:
flags |= re.I
if self.unicode_flag:
flags |= re.U
regex = re.compile(self.pattern, flags)
return regex
Please note, the self.pattern
and all the flags are class attributes defined by the user using a simple form. However, one thing I noticed in the docs is that there is usually an r
before the pattern in the compile functions, like this:
re.compile(r'(?<=abc)def')
How do I place that r
in my code before my variable name? Also, if I want to tell the user if the test input is valid or not, should I be using the match
method, or the search
method?
Thanks for any help.
Edit: This question is not a duplicate of this one, because that question has nothing to do with regular expressions.