Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:00:21+00:00 2026-06-06T12:00:21+00:00

I want to parse this CSS Selector (and others of a similar form): div.class1#myid.class2[key=value]

  • 0

I want to parse this CSS Selector (and others of a similar form):
div.class1#myid.class2[key=value]

and have it match “.class1” and “.class2” but I can’t figure out what regex to use..

example: http://www.rubular.com/r/3dxpzyJLeK

In an ideal world, I’d also want to extract the:

  • type (i.e. div)
  • class (i.e. a list of classes)
  • id (i.e myid)
  • key (i.e. key)
  • operator (i.e. =)
  • value (i.e. value)

but I can’t get the basics going!

Any help would be massively appreciated 🙂

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:00:23+00:00Added an answer on June 6, 2026 at 12:00 pm

    Thanks all very much for your suggestions and help. I tied it all together into the following two Regex Patterns:

    This one parses the CSS selector string (e.g. div#myid.myclass[attr=1,fred=3]) http://www.rubular.com/r/2L0N5iWPEJ

    cssSelector = re.compile(r'^(?P<type>[\*|\w|\-]+)?(?P<id>#[\w|\-]+)?(?P<classes>\.[\w|\-|\.]+)*(?P<data>\[.+\])*$')
    
    >>> cssSelector.match("table#john.test.test2[hello]").groups()
    ('table', '#john', '.test.test2', '[hello]')
    >>> cssSelector.match("table").groups()
    ('table', None, None, None)
    >>> cssSelector.match("table#john").groups()
    ('table', '#john', None, None)
    >>> cssSelector.match("table.test.test2[hello]").groups()
    ('table', None, '.test.test2', '[hello]')
    >>> cssSelector.match("table#john.test.test2").groups()
    ('table', '#john', '.test.test2', None)
    >>> cssSelector.match("*#john.test.test2[hello]").groups()
    ('*', '#john', '.test.test2', '[hello]')
    >>> cssSelector.match("*").groups()
    ('*', None, None, None)
    

    And this one does the attributes (e.g. [link,key~=value]) http://www.rubular.com/r/2L0N5iWPEJ:

    attribSelector = re.compile(r'(?P<word>\w+)\s*(?P<operator>[^\w\,]{0,2})\s*(?P<value>\w+)?\s*[\,|\]]')
    
    >>> a = attribSelector.findall("[link, ds9 != test, bsdfsdf]")
    >>> for x in a: print x
    ('link', '', '')
    ('ds9', '!=', 'test')
    ('bsdfsdf', '', '')
    

    A couple of things to note:
    1) This parses attributes using comma delimitation (since I am not using strict CSS).
    2) This requires patterns take the format: tag, id, classes, attributes

    The first regex does tokens, so the whitespace and ‘>’ separated parts of a selector string. This is because I wanted to use it to check against my own object graph 🙂

    Thanks again!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I want to parse this Json code : [{id:7,key:integrationContinue:integrationContinue,name:life Portlet,scope:PRJ,qualifier:TRK,date:2012-03-26T10:10:22+0100,lname:life Portlet,lang:java,version:1.0-SNAPSHOT,description:,msr:[{key:ncloc,val:897.0,frmt_val:897},{key:coverage,val:0.6,frmt_val:0,6%}]}] I created two
I have this array, that I want to parse into javascript. $offerText[] = (
I want to parse a css file and multiply each pixel value by (2/3).
In this example I have two additional css classes I want to add to
I'm receiving an xml response and I now want to parse this. Currently what
I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag
I'm trying to parse this XML I want to get a list of all
I want to parse a xml file using xquery in Ruby, I found this
I want to parse a line from a CSV(comma separated) file, something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.