android java 正则表达式命名组

android java regex named groups

我想用

matcher.group("login");

在 android 8+ on eclipse 中,但似乎不存在 Matcher.group(String)。 您有(内置的)解决方案吗?

Android Pattern class implementation is provided by ICU, to be precise, ICU4C.

The regular expression implementation used in Android is provided by ICU. The notation for the regular expressions is mostly a superset of those used in other Java language implementations. This means that existing applications will normally work as expected, but in rare cases Android may accept a regular expression that is not accepted by other implementations.

并且ICU4C目前不支持命名捕获组。您必须依靠编号的捕获组。

ICU does not support named capture groups. http://bugs.icu-project.org/trac/ticket/5312

如果您确实需要该功能,您需要自己编写包装器并解析表达式以提供命名捕获组功能。

我想我应该分享我找到的解决方案。 Github 上有一个由 Tony Trinh (tony19) 编写的很棒的库,它使人们能够使用命名的正则表达式组。

摘自项目页面:

“这个轻量级库在 Java 5/6(和 Android)中添加了对命名捕获组的支持。

这是来自 Google Code(当前不活动)的 named-regexp 项目的分支。

https://github.com/tony19/named-regexp

我刚刚在 Android 4.1.1 及更高版本上对此进行了测试,到目前为止它运行良好。我惊喜地发现我可以简单地用这个库中的 类 替换我对 Matcher 和 Pattern 的导入,并且我所有仍然使用编号组的现有正则表达式仍然有效。

希望这对您有所帮助。