Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式解析CSS选择器_Python_Regex_Css Selectors - Fatal编程技术网

Python 正则表达式解析CSS选择器

Python 正则表达式解析CSS选择器,python,regex,css-selectors,Python,Regex,Css Selectors,我想解析这个CSS选择器(以及其他类似形式的选择器): div.class1#myid.class2[key=value] 让它匹配“.class1”和“.class2”,但我不知道该用什么正则表达式 例如: 在理想世界中,我还想提取: 类型(即div) 类别(即类别列表) id(即myid) 钥匙(即钥匙) 运算符(即=) 价值(即价值) 但我没法做基本的事情 任何帮助都将不胜感激:) 谢谢 我想你需要这样的东西 (?P<tag>[a-zA-Z]+)?(\.(?P<cla

我想解析这个CSS选择器(以及其他类似形式的选择器):
div.class1#myid.class2[key=value]

让它匹配“.class1”和“.class2”,但我不知道该用什么正则表达式

例如:

在理想世界中,我还想提取:

  • 类型(即div)
  • 类别(即类别列表)
  • id(即myid)
  • 钥匙(即钥匙)
  • 运算符(即=)
  • 价值(即价值)
但我没法做基本的事情

任何帮助都将不胜感激:)


谢谢

我想你需要这样的东西

(?P<tag>[a-zA-Z]+)?(\.(?P<class>[a-zA-Z0-9_-]+)?)?(#(?P<id>[a-zA-Z0-9_-])?)?\W*\{((?P<name>[a-zA-Z0-9-_]+?)=(?P<value>[a-zA-Z0-9-_]+?))*\}
(?P[a-zA-Z]+)(\。(?P[a-zA-Z0-9-])?(?)(?(?P[a-zA-Z0-9-])?)?)\W*{(?P[a-zA-Z0-9-+?)=(?P[a-zA-Z0-9-+)*}

抱歉,如果它不起作用,我还没有测试它

我想你需要这样的东西

(?P<tag>[a-zA-Z]+)?(\.(?P<class>[a-zA-Z0-9_-]+)?)?(#(?P<id>[a-zA-Z0-9_-])?)?\W*\{((?P<name>[a-zA-Z0-9-_]+?)=(?P<value>[a-zA-Z0-9-_]+?))*\}
(?P[a-zA-Z]+)(\。(?P[a-zA-Z0-9-])?(?)(?(?P[a-zA-Z0-9-])?)?)\W*{(?P[a-zA-Z0-9-+?)=(?P[a-zA-Z0-9-+)*}

很抱歉,如果它不起作用,我还没有测试它

绝对不要尝试用一个regexp来测试它。正则表达式是出了名的难以读取和调试,因此当您完成了此任务的前80%并返回尝试修复错误时,代码将是一场噩梦

相反,尝试编写函数,甚至编写一个类,让您可以完成您想做的事情。然后,您可以为每个特定任务使用相对简单的regexp,并在实现中使用更直观的语法

class css_parser:

  def __init__(self):
    self.class_regexp = re.compile('\.[\w\-]*') # This is insufficient, but it's a start...

  def get_class(self, str):
    m = self.class_regexp.match(str)
    return m.group(0)

您需要特别参考第4节。

绝对不要试图用一个regexp来实现这一点。正则表达式是出了名的难以读取和调试,因此当您完成了此任务的前80%并返回尝试修复错误时,代码将是一场噩梦

相反,尝试编写函数,甚至编写一个类,让您可以完成您想做的事情。然后,您可以为每个特定任务使用相对简单的regexp,并在实现中使用更直观的语法

class css_parser:

  def __init__(self):
    self.class_regexp = re.compile('\.[\w\-]*') # This is insufficient, but it's a start...

  def get_class(self, str):
    m = self.class_regexp.match(str)
    return m.group(0)

您需要特别参考第4节。

非常感谢大家的建议和帮助。我将其全部绑定到以下两个正则表达式模式中:

这个函数解析CSS选择器字符串(例如div#myid.myclass[attr=1,fred=3])

cssSelector=re.compile(r'^(?P[\*\w[\\-]+)(?P[\w\\-]+)(?P\.[\w\\-]+)(?P\.[\w\\-\.]+)*(?P\[.+\])*$)
>>>cssSelector.match(“table#john.test.test2[hello]”).groups()
(‘table’、‘john’、‘test.test2’、‘hello’)
>>>cssSelector.match(“表”).groups()
(“表”,无,无,无)
>>>cssSelector.match(“table#john”).groups()
(“桌子”,“约翰”,没有,没有)
>>>cssSelector.match(“table.test.test2[hello]”).groups()
('table',None',.test.test2','[hello]')
>>>cssSelector.match(“table#john.test.test2”).groups()
('table','john','.test.test2',无)
>>>cssSelector.match(“*#john.test.test2[hello]”组()
(“*”、“#john”、“.test.test2”、“[hello]”
>>>cssSelector.match(“*”).groups()
(“*”,无,无,无)
这一个做属性(例如,[link,key~=value]):

attribSelector=re.compile(r'(?P\w+)\s*(?P[^\w\,]{0,2})\s*(?P\w+)\s*[\,\]'))
>>>a=attribSelector.findall(“[link,ds9!=test,bsdfsdf]”)
>>>对于a中的x:打印x
(“链接”,“链接”)
('ds9'、'!='、'test')
('bsdfsdf','','')
有几件事需要注意: 1) 这将使用逗号分隔来解析属性(因为我没有使用严格的CSS)。 2) 这要求模式采用以下格式:标记、id、类、属性

第一个正则表达式执行标记,因此空格和'>'分隔了选择器字符串的各个部分。这是因为我想用它来检查我自己的对象图:)


再次感谢

非常感谢大家的建议和帮助。我将其全部绑定到以下两个正则表达式模式中:

这个函数解析CSS选择器字符串(例如div#myid.myclass[attr=1,fred=3])

cssSelector=re.compile(r'^(?P[\*\w[\\-]+)(?P[\w\\-]+)(?P\.[\w\\-]+)(?P\.[\w\\-\.]+)*(?P\[.+\])*$)
>>>cssSelector.match(“table#john.test.test2[hello]”).groups()
(‘table’、‘john’、‘test.test2’、‘hello’)
>>>cssSelector.match(“表”).groups()
(“表”,无,无,无)
>>>cssSelector.match(“table#john”).groups()
(“桌子”,“约翰”,没有,没有)
>>>cssSelector.match(“table.test.test2[hello]”).groups()
('table',None',.test.test2','[hello]')
>>>cssSelector.match(“table#john.test.test2”).groups()
('table','john','.test.test2',无)
>>>cssSelector.match(“*#john.test.test2[hello]”组()
(“*”、“#john”、“.test.test2”、“[hello]”
>>>cssSelector.match(“*”).groups()
(“*”,无,无,无)
这一个做属性(例如,[link,key~=value]):

attribSelector=re.compile(r'(?P\w+)\s*(?P[^\w\,]{0,2})\s*(?P\w+)\s*[\,\]'))
>>>a=attribSelector.findall(“[link,ds9!=test,bsdfsdf]”)
>>>对于a中的x:打印x
(“链接”,“链接”)
('ds9'、'!='、'test')
('bsdfsdf','','')
有几件事需要注意: 1) 这将使用逗号分隔来解析属性(因为我没有使用严格的CSS)。 2) 这要求模式采用以下格式:标记、id、类、属性

第一个正则表达式执行标记,因此空格和'>'分隔了选择器字符串的各个部分。这是因为我想用它来检查我自己的对象图:)


再次感谢

如果你想要所有这些信息,你最好使用类似的东西。看起来已经有几个库在这样做了——而且——尽管还不清楚它们有多完整。理论上,可能有多个
[key=value]
,或者使用单独的键和值列表,或者使用包含键值对的属性列表。“标记”可能比“类型”更合适。此外,属性有更多的变体,属性值有引号和没有引号:
[type]
[type^=value]
[type$=value]
,等等,如果这很重要,那么可能需要存储