I want to find a table cell that contains the link (\d{0,3} )?pieces.
How would I need to write this xpath?
Can I simply insert the xpath directly into the Capybara search? Or do I need to do something special to indicate it is a regex? Or can I not do it at all?
Xpath 1.0
XPath 1.0 does not include regular expression support. You should be able to achieve the desired match with the following expression:
The first test in the predicate checks that the string ends with
pieces. The second test ensures that the entire string equalspieceswhen all of the digits are removed (i.e. there are no other characters). The final two tests ensure that the entire length of the string is between 6 and 9, which is the length ofpiecesplus zero to three digits.Test it on the following document:
It should match only the
test0,test1,test2, andtest3links.(Note: The expression may be further complicated by the possibility of other characters preceding the portion you’re attempting to match.)
XPath 2.0
Achieving this in XPath 2.0 is trivial with the
matchesfunction.