for string "//div[@id~'objectnavigator-card-list']//li[@class~'outbound-alert-settings']", I want to find "@..'...'" like "@id~'objectnavigator-card-list'" or "@class~'outbound-alert-settings'". But when I use regex ((@.+)\~(\'.*?\')), it find "@id~'objectnavigator-card-list']//li[@class~'outbound-alert-settings'". So how to modify the regex to find the string successfully?
-
Please format question properly.Rahul– Rahul2017-05-15 03:13:34 +00:00Commented May 15, 2017 at 3:13
Add a comment
|
3 Answers
For your current test string's input you can try this pattern:
import re
a = "//div[@id~'objectnavigator-card-list']//li[@class~'outbound-alert-settings']"
# find everything which begins by '@' and neglect ']'
regex = re.compile(r'(@[^\]]+)')
strings = re.findall(regex, a)
# Or simply:
# strings = re.findall('(@[^\\]]+)', a)
print(strings)
Output:
["@id~'objectnavigator-card-list'", "@class~'outbound-alert-settings'"]