Reputation: 361
Sorry for the bad title, but I can't find specific word to describe this question.
My test string:
MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target=.................................................................... ......................................................................MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target='super',
"u don't know anything about it" means this part is unknown, you must use .* or .*? to parse it.
My wish result: the second MethodInvocation can be paresed.
MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target='super',
My fail regexp:
MethodInvocation\(name='isOnlySingleDcAllowed', arguments=\[.*?\], type_arguments=\[.*?\], target='super'
This regexp will parse all of the result instead of the second MethodInvocation.
How can I parse it using python regexp?
Upvotes: 0
Views: 63
Reputation: 2862
Leo, give this regexp a try:
MethodInvocation\(name='isOnlySingleDcAllowed', arguments=\[[^\]]*\], type_arguments=\[[^\]]*\], target='super',
Which returns only:
"MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target='super',"
It works by ensuring that your unknown argument groups (u don't know...) terminate at the first ending bracket by excluding that character from those parts of the search.
Hope this helps.
Upvotes: 1
Reputation: 323
How about using finditer()
?
import re
s="""MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target=.................................................................... ......................................................................MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target='super',"""
ind=[]
for a in re.finditer('MethodInvocation', s):
ind.append((a.start(), a.end()))
#ind[1][0] - starting index of the second string
output = s[ind[1][0]:len(s)]
print(output)
And the output is:
MethodInvocation(name='isOnlySingleDcAllowed', arguments=["u don't know anything about it"], type_arguments=["u don't know anything about it"], target='super',
Upvotes: 0