Reputation: 107
I have strings like "members[value eq\"2819c223-7f76-453a-919d-413861904646\"].displayName"
, where i was trying to retrieve members, string in bracket, and displayname.
I tried this, matchObj = re.match( r"(.*)\[([.*]+)\?](.*?)", line, re.M|re.I)
where string in bracket and last string(after .
) are optional (this part "[value eq\"2819c223-7f76-453a-919d-413861904646\"].displayName"
).
What am I doing wrong here?
Upvotes: 2
Views: 52
Reputation: 23667
(\w+)(\[[^]]*\])?(\.\w+)?
\w
will match any word character (alphabets, numbers and underscore)
[a-zA-Z]
to match only alphabets, [a-zA-Z\d]
to match digits as well\[[^]]*\]
will match [
followed by non ]
characters followed by ]
(\[[^]]*\])?
makes it optional for this entire pattern\.\w+
will match .
and word charactersIf you use (.*)
instead of (\w+)
the greediness of .*
will make it to match everything as the next two are optional. See also: Greedy vs. Reluctant vs. Possessive Quantifiers
Upvotes: 1