Skip to content

Explore using a regular expression selector #175

Closed

Description

@facelessuser

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.

@gir-bot add T: feature, S: maybe
@gir-bot lgtm

Activity

  1. added
    S: triageIssue needs triage.
    and removed
    S: triageIssue needs triage.
    on Jan 25, 2020
  2. facelessuser commented on Jan 25, 2020

    @facelessuser
    OwnerAuthor

    Currently, within the bounds of /.../ it is Python regex syntax. This means no CSS escapes etc., just pure Python regex.

  3. facelessuser commented on Jan 26, 2020

    @facelessuser
    OwnerAuthor

    Experimental pull: #176.

  4. facelessuser commented on Jan 27, 2020

    @facelessuser
    OwnerAuthor

    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.

  5. facelessuser commented on Nov 17, 2020

    @facelessuser
    OwnerAuthor

    While interesting, we won't be approaching regex selectors in this way. If/when we do, it will likely use custom pseudo classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P: maybePending approval of low priority request.T: featureFeature.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions