isah
isah

Reputation: 5351

Regex extract last numbers from String

I have some strings which are indexed and are dynamic. For example:

name01,
name02,
name[n]

now I need to separate name from index. I've come up with this regex which works OK to extract index.

([0-9]+(?!.*[0-9]))

But, there are some exceptions of these names. Some of them may have a number appended which is not the index.(These strings are limited and I know them, meaning I can add them as "exceptions" in the regex)

For example,

panLast4[01]

Here the last '4' is not part of the index, so I need to distinguish. So I tried:

[^panLast4]([0-9]+(?!.*[0-9]))

Which works for panLast4[123] but not panLast4[43]

Note: the "[" and "]" is for explanation purposes only, it's not present in the strings

What is wrong?

Thanks

Upvotes: 1

Views: 1582

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

You can use the split method with this pattern:

(?<!^panLast(?=4)|^nm(?=14)|^nm1(?=4))(?=[0-9]+$)

The idea is to find the position where there are digits until the end of the string (?=[0-9]+$). But the match will succeed if the negative lookbehind allows it (to exclude particular names (panLast4 and nm14 here) that end with digits). When one of these particular names is found, the regex engine must go to the next position to obtain a match.

Example:

String s ="panLast412345";
String[] res = s.split("(?<!^panLast(?=4)|^nm(?=14)|^nm1(?=4))(?=[0-9]+$)", 2);
if ( res.length==2 ) {
    System.out.println("name: " + res[0]);
    System.out.println("ID: " + res[1]); 
}

An other method with matches() that simply uses a lazy quantifier as last alternative:

Pattern p = Pattern.compile("(panLast4|nm14|.*?)([0-9]+)");
String s = "panLast42356";
Matcher m = p.matcher(s);
if ( m.matches() && m.group(1).length()>0 ) {
    System.out.println("name: "+ m.group(1));
    System.out.println("ID: "+ m.group(2));
}

Upvotes: 2

Related Questions