I'm writing a small helper script to analyse C code, especially the use of structs. I have problems detecting when a struct is used as a value as opposed to a pointer. That means I want to detect if the text struct foo
is followed by an arbitrary amount of whitespace and a character that is not *
.
I boiled my problem down to this MWE:
>>> import re
>>> there = re.compile('struct foo(\\s*)[^*]')
>>> match = there.search('struct foo *bar')
Note. I need to use the double backslash because I cannot use raw strings in my application. I actually need an f-string.
The MWE should not produce a match in my book. However, it does and if I look at match.groups()
, I get
>>> match.groups()
('',)
meaning that \\s*
did match zero whitespace characters. From the documentation I would have expected it to match the single space before *foo
in my string as the *
quantifier should match zero or more characters greedily.
Exchanging \\s
with [ \t]
or even *
(note the space) does not make a difference either.
Why does \\s*
seem to match zero characters in presence of a space?