Android java regex named groups

I would like to use

matcher.group("login"); 

On android 8+ on eclipse, but Matcher.group (String) doesn't seem to exist. Do you have an integrated solution?

+6
source share
2 answers

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

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

And ICU4C does not currently support a name capture group. You must retreat from the numbered capture groups.

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

You need to write a wrapper and parse the expression yourself to provide the ability to group captured groups if you really need this feature.

+8
source

I thought I should share the solution I found. Github has a fantastic library written by Tony Trin (tony19) that allows the use of named regex groups.

Taken from the project page:

"This lightweight library adds support for named capture groups in Java 5/6 (and Android).

This is a fork of the named-regexp project from Google Code (currently inactive).

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

I just tested this on Android 4.1.1 and above, and so far it works like a charm. I was pleasantly surprised to find that I can simply replace my import for Matcher and Pattern with classes from this library, and all my existing regular expressions that still used numbered groups still worked.

I hope this helps.

+2
source

Source: https://habr.com/ru/post/980688/


All Articles