2
According to documentation, re.match
never allows searching at the beginning of a line:
Note that even in
MULTILINE
mode,re.match()
will only match at the beginning of the string and not at the beginning of each line.
You need to use a re.search
:
regexString = r"^\s*"+item.feature_key+":"
pq = re.compile(regexString, re.M)
if pq.search(data):
A small note on the raw string (r"^\s+"
): in this case, it is equivalent to "\s+"
because there is no \s
escape sequence (like \r
or \n
), thus, Python treats it as a raw string literal. Still, it is safer to always declare regex patterns with raw string literals in Python (and with corresponding notations in other languages, too).
Source:stackexchange.com