Repository navigation
Explore using a regular expression selector #175
Description
Explore the possibility of allowing a regex selector of our own making. There is no guarantee that it won't one day conflict with some later CSS feature, but currently the idea is to build it around the form of /pattern/.
Currently, we have an experimental branch that employs this logic inside :contains() and [attributes] as the value portion: :contains(/pattern/), [attributes=/pattern/], and even in [attributes$=/pattern/] etc. https://github.com/facelessuser/soupsieve/tree/experimental/regex
Not sure if these should bother respecting the documents preference for case sensitivity yet, or simply just assume case insensitive (default for regex) unless forced into insensitivity.
I'm not sure how far the experiment will go, but we may extend this test into matching tag names, namespaces, and potentially attribute names (not just values): /ns/|/tag/[/name/=/value/]:contains(/text/). I don't think we'd go beyond that. We would also be very selective of what pseudo-classes support it, :contains() being the only one right now. Though, you could still do things like :not(/tag/:contains(/text/)). I more mean that pseudo-classes with specialized syntax would not necessarily support such behavior, such as :lang() etc.
There is no promise that this will become a real feature, but we are currently exploring this as a possibility. I often find select in BeautifulSoup more enjoyable to use than find_all. This is mainly because I find a bit more cumbersome, and I don't always like how it compares class attributes. The ability to stay within CSS selectors without having to resort to find_all would be nice, and powerful.
We of course run the risk that CSS will develop some syntax that clashes with our /pattern/ syntax. This cannot be known, and we run a risk if do implement it, but it is also possible that even if they use / it will be done in such a way that it will not conflict with how we use it. We could adopt some syntax like re(/pattern/) if we are really concerned, or in the future if forced into a corner one day by the CSS spec.
Activity
facelessuser commentedon Jan 25, 2020
Currently, within the bounds of
/.../it is Python regex syntax. This means no CSS escapes etc., just pure Python regex.facelessuser commentedon Jan 26, 2020
Experimental pull: #176.
facelessuser commentedon Jan 27, 2020
While this is interesting, I'm not so sure I'm sold on needing regular expression for namespaces, tag names, and attribute names...I could just limit this to
:contains(/pattern/)and[attr=/pattern/]. While I'm sure if we added it, people may use it for tag names or namespaces etc. But I'm not sure if it is something that is needed.We could just do something like
:re-contains(/pattern/)and:re-attribute(name=/pattern/). It's honestly easier to contain. Right now supporting things as they are in our experimental branch sprawls out everywhere. This would honestly be more compact and easier to maintain.facelessuser commentedon Nov 17, 2020
While interesting, we won't be approaching regex selectors in this way. If/when we do, it will likely use custom pseudo classes.