Home:ALL Converter>python regex xpath - regex an attribute of XPATH

python regex xpath - regex an attribute of XPATH

Ask Time:2019-03-24T17:27:51         Author:Rahmat Nazali Salimi

Json Formatter

Given a flexible string of regex pattern, I need to find all attributes attached.

Example string: /html/body/div[1]/div/a/(@title|@href)

It needs to return ['@title', '@href']

I did some research and created a regex pattern like this: /@\w+/g

Tried it on regex101 and it did seems to works: https://regex101.com/r/cO8lqs/9124

But when I coded it in python

import re
xpath = "/html/body/div[1]/div/a/(@title|@href)"

print(re.findall("/@\w+/g", xpath)) # should have been worked

It returns []

As mentioned above, it needs to return ['@title', '@href']

Did I missed something?

Author:Rahmat Nazali Salimi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55322349/python-regex-xpath-regex-an-attribute-of-xpath
Rahmat Nazali Salimi :

As suggested by @FailSafe on the question's comment, turns out I need to change the regex pattern from /@\\w+/g to @\\w+.",
2019-03-24T09:37:43
yy