在Python中分割标记上的文本

mfuanj7w  于 2023-09-29  发布在  Python
关注(0)|答案(3)|浏览(168)

我有以下一行文字:

  1. <code>stuff</code> and stuff and $\LaTeX$ and <pre class="mermaid">stuff</pre>

使用Python,我想打破标记实体以获得以下列表:

  1. ['<code>', 'stuff', '</code>', ' and stuff and $\\LaTeX$ ', '<pre class="mermaid">', 'stuff', '</pre>']

到目前为止,我使用了:

  1. markup = re.compile(r"(<(?P<tag>[a-z]+).*>)(.*?)(<\/(?P=tag)>)")
  2. text = '<code>stuff</code> and stuff and $\LaTeX$ and <pre class="mermaid">stuff</pre>'
  3. words = re.split(markup, text)

但它产生:

  1. ['<code>', 'code', 'stuff', '</code>', ' and stuff and $\\LaTeX$ ', '<pre class="mermaid">', 'pre', 'stuff', '</pre>']

问题是(?P=tag)组被添加到列表中,因为它被捕获了。我捕捉它只是为了得到最接近的结束标签。
假设代码一次只处理一行,我如何在结果列表中去掉它?

qyzbxkaa

qyzbxkaa1#

您可以使用xml,它是为xml files设计的模块,与html同义。

  1. import xml.etree.ElementTree as ET
  2. text = '<code>stuff</code> and stuff and $\LaTeX$ and <pre class="mermaid">stuff</pre>'
  3. root = ET.fromstring(f'<root>{text}</root>')
  4. result = []
  5. for element in root:
  6. if element.tag:
  7. result.append(f'<{element.tag}>')
  8. if element.text:
  9. result.extend(element.text.split())
  10. if element.tail:
  11. result.append(element.tail)
  12. print(result)
展开查看全部
a11xaf1n

a11xaf1n2#

RegEx不适合解析HTML。然而,它通常足以用于令牌化。使用re.finditer,令牌化变成了一行代码:

  1. list(map(lambda x: x.group(0), re.finditer(r"(?:<(?:.*?>)?)|[^<]+", s)))

说明:

  • 仅使用非捕获组(?:...);我们这里不需要具体的捕获。
  • 匹配一个“标签”<(?:.*?>)?(可能是无效的(只是<符号),仅通过其开口<识别,直到>)或明文[^<]+

这将处理您的测试用例

  1. s = '<code>stuff</code> and stuff and $\LaTeX$ and <pre class="mermaid">stuff</pre>'

正确地,生产

  1. ['<code>', 'stuff', '</code>', ' and stuff and $\\LaTeX$ and ', '<pre class="mermaid">', 'stuff', '</pre>']

然而,请注意,一个成熟的HTML标记器需要一个更复杂的常规语法来处理,例如。onclick = "console.log(1 < 2)"等属性。最好使用现成的库来为您进行标记解析(甚至只是标记化)。

展开查看全部
von4xj4u

von4xj4u3#

  1. s = r'<code>stuff</code> and stuff and $\LaTeX$ and <pre class="mermaid">stuff</pre>'
  2. l = []
  3. for i in range(len(s)):
  4. if s[i] == ">":
  5. l[-1] += s[i]
  6. l.append("")
  7. elif s[i] == "<":
  8. l.append("")
  9. l[-1] += s[i]
  10. else:
  11. l[-1] += s[i]
  12. l.pop()
  13. print(l)

输出:['<code>', 'stuff', '</code>', ' and stuff and $\\LaTeX$ and ', '<pre class="mermaid">', 'stuff', '</pre>']

展开查看全部

相关问题