Mohan
Mohan

Reputation: 107

Match the substring inside a string with escaped quotes using regexp

I have string like this,

filter = "emails.type==\"work\""

The regexp i have written to group different things,

matchobj = re.search(r"(\w+)(\.)?(\w+)?(==|!=|(like|LIKE))(.*[^\\])", filter)

testing,

print("group1 ", matchobj.group(1))
    print("group2 ", matchobj.group(2))
    print("group3 ", matchobj.group(3))
    print("group4 ", matchobj.group(4))
    print("group5 ", matchobj.group(5))

result,

group1  emails
group2  .
group3  type
group4  ==
group5  None

The issue is with group 5, which is returning None and expected value for group 5 is "work" with quotes.

Upvotes: 0

Views: 26

Answers (1)

AlexisG
AlexisG

Reputation: 2484

you have to remove the parenthesis around like|LIKE because it creates another capturing group. So you have 6 groups instead of 5.

(\w+)(\.)?(\w+)?(==|!=|like|LIKE)(.*[^\\])

try it

Upvotes: 2

Related Questions