0

I need a last alphabetic character of string example: ABRACADABRA123456. The regex expression [a-zA-Z](?=\d+) give me the match in all my cases. How can I change (inverse) the expression to use it in java method e.g.: "ABRACADABRA123456".replaceAll(<inverse-regex>,"")?

INPUT:ABRACADABRA123456
USE:"ABRACADABRA123456".replaceAll(...)
OUTPUT:A (a last alphabetic character of string)

RESOLVED:System.out.println("ABRACADABRA123456".replaceAll("([\\D]+)([a-zA-Z](?=\\d+))([\\d]+)","$2")));

usmandam
  • 1
  • 1
  • 3
    What's your expected output? – Avinash Raj Feb 11 '15 at 11:10
  • expected output a alphabetic character in parentheses: `ABRACADABR(A)123456` – usmandam Feb 11 '15 at 11:22
  • @usmandam If the original regex is ‘a letter before some digits’, then what is the ‘inverse’? A letter *after* some digits? A letter *not* followed by digits? A digit before some letters? I could go on... – Biffen Feb 11 '15 at 11:22

1 Answers1

0

[a-zA-Z](?=\d+) won't match the last alphabetic character.

System.out.println("ABRACADABRA123456".replaceAll("([A-Za-z])(?=[^A-Za-z]*$)","($1)"));

The above regex would capture the alphabet only if it's followed by any non-alphabetic character zero or more times until the last. So it matches only the last alphabet.

Output:

ABRACADABR(A)123456
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274