Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks all very much for your suggestions and help. I tied it all together into the following two Regex Patterns:</p> <p>This one parses the CSS selector string (e.g. div#myid.myclass[attr=1,fred=3]) <a href="http://www.rubular.com/r/2L0N5iWPEJ" rel="nofollow">http://www.rubular.com/r/2L0N5iWPEJ</a></p> <pre><code>cssSelector = re.compile(r'^(?P&lt;type&gt;[\*|\w|\-]+)?(?P&lt;id&gt;#[\w|\-]+)?(?P&lt;classes&gt;\.[\w|\-|\.]+)*(?P&lt;data&gt;\[.+\])*$') &gt;&gt;&gt; cssSelector.match("table#john.test.test2[hello]").groups() ('table', '#john', '.test.test2', '[hello]') &gt;&gt;&gt; cssSelector.match("table").groups() ('table', None, None, None) &gt;&gt;&gt; cssSelector.match("table#john").groups() ('table', '#john', None, None) &gt;&gt;&gt; cssSelector.match("table.test.test2[hello]").groups() ('table', None, '.test.test2', '[hello]') &gt;&gt;&gt; cssSelector.match("table#john.test.test2").groups() ('table', '#john', '.test.test2', None) &gt;&gt;&gt; cssSelector.match("*#john.test.test2[hello]").groups() ('*', '#john', '.test.test2', '[hello]') &gt;&gt;&gt; cssSelector.match("*").groups() ('*', None, None, None) </code></pre> <p>And this one does the attributes (e.g. [link,key~=value]) <a href="http://www.rubular.com/r/2L0N5iWPEJ" rel="nofollow">http://www.rubular.com/r/2L0N5iWPEJ</a>:</p> <pre><code>attribSelector = re.compile(r'(?P&lt;word&gt;\w+)\s*(?P&lt;operator&gt;[^\w\,]{0,2})\s*(?P&lt;value&gt;\w+)?\s*[\,|\]]') &gt;&gt;&gt; a = attribSelector.findall("[link, ds9 != test, bsdfsdf]") &gt;&gt;&gt; for x in a: print x ('link', '', '') ('ds9', '!=', 'test') ('bsdfsdf', '', '') </code></pre> <p>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</p> <p>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 :)</p> <p>Thanks again!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload