如何使用正则表达式获取字符串中最后一个逗号之后的内容?
示例:
abcd,fg;ijkl, cas
输出应为cas
注意:最后一个逗号和'c'
字符之间有一个空格,也需要删除。此外,该模式在最后一个逗号后仅包含一个空格。
发布于 2012-03-01 19:44:33
可能是这样的:
String s = "abcd,fg;ijkl, cas";
String result = s.substring(s.lastIndexOf(',') + 1).trim();
也就是说,我取最后一个逗号后面的子字符串,然后删除周围的空格...
发布于 2012-03-01 19:44:45
你可以试试这个:
public static void main(String[] args) {
String s = " abcd,fg;ijkl, cas";
String[] words = s.split(",");
System.out.println(words[words.length-1].trim());
}
发布于 2012-03-01 19:45:21
只有一个空格:
String[] aux = theInputString.split(",\\s");
string result = aux[aux.length-1];
0到n个空格:
String[] aux = theInputString.split(",\\s*");
string result = aux[aux.length-1];
https://stackoverflow.com/questions/9515505
复制相似问题