如何为 HTML 简单文本制作正则表达式模式?
我正在尝试学习课程的正则表达式模式。我正在制作一个简单的 HTML 词法分析器/解析器。我知道这不是制作词法分析器/解析器的最佳或最有效的方法,但它只是为了理解正则表达式模式。
所以我的问题是,如何创建一个模式来检查字符串是否不包含任何 HTML 标记(即
)并且不包含任何 HTML 实体(即 &耳鼻喉科;
)?
这是我到目前为止可以想到的,但它仍然不起作用:
.+?(^(?:&[A-Za-z0-9#]+;)^(?:<.*?>))
编辑:唯一的问题是我不能否定最终结果我需要找到一个完整的模式来完成这个任务如果可能的话,尽管可能不太好。我从未提到过,但它几乎应该匹配 HTML 页面中的任何简单文本。
I am trying to learn Regex patterns for a class. I am making a simple HTML Lexer/Parser. I know this is not the best or most efficient way to make a Lexer/Parser but it is only to understand Regex patterns.
So my question is, How do I create a pattern that checks if the String does not contain any HTML tags (ie <TAG>
) and does not contain any HTML Entities (ie &ENT;
)?
This is what I could come up with so far but it still does not work:
.+?(^(?:&[A-Za-z0-9#]+;)^(?:<.*?>))
EDIT: The only problem is that I can't negate the final outcome I need to find a complete pattern that would accomplish this task if it's possible, although it might not be pretty. I never mentioned but it's pretty much supposed to match any Simple Text in an HTML page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用表达式
<.+?>|&.+?;
搜索匹配项,然后对结果求反。<.+?>
首先表示<
,然后是任何内容(一次或多次),然后是>
& .+?;
首先表示&
,然后是任何内容(一次或多次),然后是;
这是一个带有 ideone.com 演示在这里。
输出:
You could use the expression
<.+?>|&.+?;
to search for a match, and then negate the result.<.+?>
says first a<
then anything (one or more times) then a>
&.+?;
says first a&
then anything (one or more times) then a;
Here is a complete example with an ideone.com demo here.
Output:
如果您要匹配不遵循模式的字符串,最简单的方法是匹配模式,然后否定测试结果。
与此模式匹配的任何字符串都将具有至少一个标记(如您所定义的)或实体(如您所定义的)。因此,您想要的字符串是与此模式不匹配的字符串(它们没有标签或实体)。
If you're looking to match strings that do NOT follow a pattern, the simplest thing to do is to match the pattern and then negate the result of the test.
Any string that matches this pattern will have AT LEAST ONE tag (as you've defined it) or entity (as you've defined it). So the strings you want are strings that DO NOT match this pattern (they will have NO tags or entities).