Mohan
Mohan

Reputation: 107

Retrieve sections from string using regular expressions in python

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

Answers (1)

Sundeep
Sundeep

Reputation: 23667

(\w+)(\[[^]]*\])?(\.\w+)?
  • \w will match any word character (alphabets, numbers and underscore)
    • use [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 characters

If 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

Related Questions