user621819
user621819

Reputation:

How do i use regular expressions in python lxml, XPath

I'm trying to do:

for element in root.xpath('//a[@id="hypProduct_[0-9]+"]'):

How do i use [0-9]+ within an xpath element selector (lxml)? The docs state:

By default, XPath supports regular expressions in the EXSLT namespace:

>>> regexpNS = "http://exslt.org/regular-expressions"
>>> find = etree.XPath("//*[re:test(., '^abc$', 'i')]",
...                    namespaces={'re':regexpNS})

>>> root = etree.XML("<root><a>aB</a><b>aBc</b></root>")
>>> print(find(root)[0].text)
aBc

You can disable this with the boolean keyword argument regexp which defaults to True.

I didn't follow the :test stuff. Could someone explain this in context of the docs.

Upvotes: 3

Views: 3186

Answers (1)

alecxe
alecxe

Reputation: 474191

In your case, the expression would be:

//a[re:test(@id, "^hypProduct_[0-9]+$")]

Demo:

>>> from lxml.html import fromstring
>>> 
>>> data = '<a id="hypProduct_10">link1</a>'
>>> tree = fromstring(data)
>>> tree.xpath('//a[re:test(@id, "^hypProduct_[0-9]+$")]', namespaces={'re': "http://exslt.org/regular-expressions"})[0].attrib["id"]
'hypProduct_10'

Upvotes: 6

Related Questions