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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I agree that using the standard String.lastIndexOf() method is your best course of action, but I have recently had use for the Regex part (namely, I wanted to find the last non-alphanumeric character in a string).
I ended up writing it myself, and thought to share, in hopes that it would serve to help others:
Also, it may be possible to make this method faster by first reversing the input string, then taking the ending index of the first group (rather than going over all the groups).
But to do that you would have to reverse the pattern as well; that can be simple in some cases (like my case of searching for a single character), but may prove problematic in others.
The core question is good although the example you gave doesn't need it. Java's indexOf doesn't take regular expressions. Answering just subject part of the question, here's what you would need:
If you need the last index:
If all you want is to find the last instance of a character regex is overkill, you should just use String's lastIndexOf
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:
ref: https://github.com/apache/commons-lang/pull/273/files