How can I get the last integer "56" from String like ra12ke43sh56?
I have to modify the next value as ra12ke43sh57 so I want to get the last the integer value.
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i --) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
sb.insert(0, c);
} else {
break;
}
}
String result = sb.toString();
or
Pattern p = Pattern.compile("[0-9]+$");
Matcher m = p.matcher(str);
if(m.find()) {
result = m.group();
}
And then Integer.parseInt(result)
Perhaps something like this is what you want (see also on ideone.com):
static String nextId(String id) {
String[] parts = id.split("(?=\\d+$)", 2);
final int L = parts[1].length();
final int num = Integer.parseInt(parts[1]) + 1;
return parts[0] + String.format("%0"+L+"d", num);
}
public static void main(String[] args) {
String[] tests = {
"jamesBond007", "ra12ke43sh57", "-42", "x888y999", "00000"
};
for (String test : tests) {
System.out.println(nextId(test));
}
// prints "jamesBond008", "ra12ke43sh58", "-43", "x888y1000", "00001"
}
There are several things at work here:
String.split(String regex, int limit)
, limited to 2 parts
parts[0]
is the static prefix, possibly emptyparts[1]
is the sequence of digits at the end of the string(?=\d+$)
matches a position where you can match \d+$
\d
character class+
$
String.format
to preserve any leading zeroes
%05d
means:
0
: pad with leading zeroes5
: width is 5d
: decimal integer conversionOn lookarounds:
(?<=#)[^#]+(?=#)
work?Not an answer, just to mention that the "obvious" regular expressions for this (as posted above) have a nasty quadratic behavior in the worst case (I'm pretty sure you are not dealing with such degenerate cases though, but it's interesting in its own accord).
Here is a little graph I prepared: alt text http://img517.imageshack.us/img517/1158/imageerp.jpg
I used strings of the form "111..1111a2"
There are J
occurrences of '1'.
The reason of the bad performance is obvious: the matcher keeps seeing 1s and happily thinks it's going to succeed, and only when it finds a
nearly the end it finds that a
doesn't match $
, so it goes all the way back, and starts at the second character.
Initially I thought that possessive quantifiers would help here, but it doesn't look straight-forward. Even if I write \d++
, if I call matcher.find() it will still try to match the pattern starting from every index anyway, i.e. the outer loop takes no hint from the previous fails of possesive quantifiers. (Note this issue does not affect Matcher.matches() since that only tries to match at a single index, the start).
In this particular problem, to avoid this behavior we would have to reverse the string and search with a simple ^\\d+
pattern, or iterate the characters backwards and do a manual match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With