Personally, I don't use re.VERBOSE
because I don't like to escape the blank spaces and I don't want to put '\s' instead of blank spaces when '\s' isn't required.
The more the symbols in a regex pattern are precise relatively to the characters sequences that must be catched, the faster the regex object acts. I nearly never use '\s'
.
To avoid re.VERBOSE
, you can do as it has been already said:
test = re.compile(
'(?P<full_path>.+)'
':\d+:\s+warning:\s+Member\s+' # comment
'(?P<member_name>.+)'
'\s+\('
'(?P<member_type>%s)' # comment
'\) of '
'(class|group|namespace)'
# ^^^^^^ underlining something to point out
'\s+'
'(?P<class_name>.+)'
# vvv overlining something important too
'\s+is not documented'\
% (self.__MEMBER_TYPES),
re.IGNORECASE)
Pushing the strings to the left gives a lot of space to write comments.
.
But this manner isn't so good when the pattern is very long because it isn't possible to write
test = re.compile(
'(?P<full_path>.+)'
':\d+:\s+warning:\s+Member\s+' # comment
'(?P<member_name>.+)'
'\s+\('
'(?P<member_type>%s)' % (self.__MEMBER_TYPES) # !!!!!! INCORRECT SYNTAX !!!!!!!
'\) of '
'(class|group|namespace)'
# ^^^^^^ underlining something to point out
'\s+'
'(?P<class_name>.+)'
# vvv overlining something important too
'\s+is not documented',
re.IGNORECASE)
then in case the pattern is very long, the number of lines between
the part % (self.__MEMBER_TYPES)
at the end
and the string '(?P<member_type>%s)'
to which it is applied
can be big and we loose the easiness in reading the pattern.
.
That's why I like to use a tuple to write a very long pattern:
pat = ''.join((
'(?P<full_path>.+)',
# you can put a comment here, you see: a very very very long comment
':\d+:\s+warning:\s+Member\s+',
'(?P<member_name>.+)',
'\s+\(',
'(?P<member_type>%s)' % (self.__MEMBER_TYPES), # comment here
'\) of ',
# comment here
'(class|group|namespace)',
# ^^^^^^ underlining something to point out
'\s+',
'(?P<class_name>.+)',
# vvv overlining something important too
'\s+is not documented'))
.
This manner allows to define the pattern as a function:
def pat(x):
return ''.join((\
'(?P<full_path>.+)',
# you can put a comment here, you see: a very very very long comment
':\d+:\s+warning:\s+Member\s+',
'(?P<member_name>.+)',
'\s+\(',
'(?P<member_type>%s)' % x , # comment here
'\) of ',
# comment here
'(class|group|namespace)',
# ^^^^^^ underlining something to point out
'\s+',
'(?P<class_name>.+)',
# vvv overlining something important too
'\s+is not documented'))
test = re.compile(pat(self.__MEMBER_TYPES), re.IGNORECASE)
re.VERBOSE
example(?x)
by enabling syntax highlighting. Also you could use[ ]
or\
to escape a space.