在Java中查找Regex的最新索引 [英] Find Last Index Of by Regex in Java

查看:94
本文介绍了在Java中查找Regex的最新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串%/ O ^ / O%/ O 。我想找到最后一个/分割字符串。第一个尝试是: \ / [POL] $ 但这包括O这是显而易见的。有人提示吗?

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?

推荐答案

你需要使用正则表达式吗?将String。 lastIndexOf(/)用于查找索引,然后使用 String.substring(int start,int end),结果如何?或者您的实际数据是否不同且更复杂,需要正则表达式?用你提供的来分割最后一个/的字符串,这里是代码:

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);

这篇关于在Java中查找Regex的最新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆