如何在Delphi中实现一套标准的超链接检测规则
我目前在程序中自动检测文本中的超链接。我做得非常简单,只查找 http:// 或 www。
但是,一位用户建议我将其扩展为其他形式,例如: https:// 或 .com
然后我意识到它可能不会就此停止,因为还有 ftp、mailto 和 file、所有其他顶级域,甚至电子邮件地址和文件路径。
我认为最好的方法是遵循当前正在使用的一些常用的标准超链接检测规则集,将其限制在实用范围内。也许 Microsoft Word 是如何做到的,或者 RichEdit 是如何做到的,或者您可能知道更好的标准。
所以我的问题是:
是否有一个内置函数可以从 Delphi 调用来进行检测,如果有,调用会是什么样子? (我计划将来使用 FireMonkey,所以我更喜欢能够在 Windows 之外工作的东西。)
如果没有可用的功能,是否可以在某个地方找到一组记录在 Word 中检测到的规则,在 RichEdit 中,或者任何其他应该检测什么规则集?这样我就可以自己编写检测代码。
I currently do automatic detection of hyperlinks within text in my program. I made it very simple and only look for http:// or www.
However, a user suggested to me that I extend it to other forms, e.g.: https:// or .com
Then I realized it might not stop there because there's ftp and mailto and file, all the other top level domains, and even email addresses and file paths.
What I think is best is to limit it to what is practical by following some often-used standard set of hyperlink detection rules that are currently in use. Maybe how Microsoft Word does it, or maybe how RichEdit does it or maybe you know of a better standard.
So my question is:
Is there a built in function that I can call from Delphi to do the detection, and if so, what would the call look like? (I plan in the future to go to FireMonkey, so I would prefer something that will work beyond Windows.)
If there isn't a function available, is there some place I can find a documented set of rules of what is detected in Word, in RichEdit, or any other set of rules of what should be detected? That would then allow me to write the detection code myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
PathIsURL
函数,该函数在ShLwApi
单元。Try the
PathIsURL
function which is declarated in theShLwApi
unit.遵循从 RegexBuddy 库中获取的正则表达式可能会让您入门(我无法对性能做出任何声明)。
正则表达式
说明
匹配(全部或部分)
不匹配
对于一组规则,您可以查看RFC 3986
验证 RFC 3986 中指定的 URL 的正则表达式是
Following regex taken from RegexBuddy's library might get you started (I can't make any claims about performance).
Regex
Explanation
Matches (whole or partial)
Does not match
For a set of rules you might look into RFC 3986
A regex that validates a URL as specified in RFC 3986 would be
正则表达式可能是这里的方法,用于定义您认为合适的超链接的各种模式。
Regular Expressions may be the way to go here, to define the various patterns which you deem to be appropriate hyperlinks.