tags:

views:

46

answers:

4

Hello,

i have a string %/O^/O%/O. I want to find the last / to split the string. First attemp was: \/[POL]$ but that gets it inclusive the "O" which is obvious. Has somebody a tip?

A: 
/(?=[^/]*$)

will match a / that isn't followed by any more /s. To split on it, use

String[] splitArray = subjectString.split("/(?=[^/]*$)");
Tim Pietzcker
+2  A: 

If all you want is to find the last instance of a character regex is overkill, you should just use String's lastIndexOf

int pos = myString.lastIndexOf('/');
M. Jessup
+1 - this is more efficient. On the other hand, a regex that finds the last `/` can be fed directly to `.split()` - it's probably personal taste as to which is more readable (compare my solution with justkt's).
Tim Pietzcker
+1  A: 

Do you need to use regular expressions for this? Would String.lastIndexOf("/") work to find the index, and then use String.substring(int start, int end) with the result? Or is your actual data different and more complicated, requiring regular expressions? With what you provided to split the string on the last /, here's code:

int lastSlash = mystring.lastIndexOf("/");
String start = mystring.substring(0, lastSlash);
String end = mystring.substring(lastSlash + 1, mystring.length);
justkt
A: 

thx thats it. Thank you very much

Andreas
@Andreas - depending on which answer solved your problem, suggest checking the check box next to that answer to mark it as "accepted" and provide to the community information on which solution worked best for you.
justkt