Patterns.WEB_URL.matcher(str)的错误行为 [英] Wrong behavior of Patterns.WEB_URL.matcher(str)

查看:177
本文介绍了Patterns.WEB_URL.matcher(str)的错误行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在我的应用程序中使用 Patterns.WEB_URL.matcher(str)来验证我应用程序中的URL.此api在Lollipop之前的API中非常有效.但是,在Lollipop中,它甚至可以验证"abd.def".奇怪和越野车的行为.有任何修复建议/想法吗?

I've beed using Patterns.WEB_URL.matcher(str) in my app to validate the URLs in my app. This api works great in pre-Lollipop APIs. However, in Lollipop it is validating even "abd.def". Strange and buggy behavior. Any suggestions/idea of a fix?

推荐答案

奇怪和越野车的行为

Strange and buggy behavior

根据定义,使用固件烘焙的正则表达式为"buggy".

Using a firmware-baked regex is by definition "buggy".

顶级域(TLD)的列表一直在稳步扩大.届时任何试图限制已知TLD的正则表达式最终都会过时.因此,在12个月前,Google工程师将该正则表达式更改为接受任何TLD,只要它使用正确的字符即可.替代方案是一系列永无止境的错误报告,这些错误报告涉及有效URL如何使正则表达式失败(当发布该版本的Android时,这些URL将无效).

The list of top-level domains (TLDs) has been steadily expanding. Any regex that tries to limit to known TLDs at that time will become out of date eventually. So, 12 months ago, Google engineers changed the regex to accept any TLD, so long as it uses the proper characters. The alternative is a never-ending series of bug reports about how valid URLs fail the regex, when those URLs would have been invalid when that version of Android shipped.

有什么建议/想法解决吗?

Any suggestions/idea of a fix?

编写您自己的正则表达式.或者,派生旧版正则表达式并维护自己的有效TLD列表.或者,通过其他方式验证URL.或者,找人积极维护一个允许您执行相同操作的库,在此情况下,请确保不断更新对库的使用,以考虑新的TLD.

Write your own regex. Or, fork the older regex and maintain your own list of valid TLDs. Or, validate the URLs by some other means. Or, find somebody actively maintaining a library that allows you to do the same, where you make sure that you keep updating your use of the library to take into account new TLDs.

IOW,请执行与固件无关的操作,并确保您保持其有效性,以保持有效TLD的变化.

IOW, do something that is firmware-independent, and make sure you keep maintaining it to be accurate with respect to changes in valid TLDs.

这篇关于Patterns.WEB_URL.matcher(str)的错误行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆