34

How do I get the content after the last comma in a string using a regular expression?

Example:

abcd,fg;ijkl, cas

The output should be cas


Note: There is a space between last comma and 'c' character which also needs to be removed. Also the pattern contains only one space after last comma.

dacwe
  • 43,066
  • 12
  • 116
  • 140
Nitish
  • 834
  • 4
  • 15
  • 26

9 Answers9

55

Using regular expressions:

Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher("abcd,fg;ijkl, cas");

if (m.find())
    System.out.println(m.group(1));

Outputs:

cas

Or you can use simple String methods:

  1. System.out.println(s.substring(s.lastIndexOf(",") + 1).trim());
  2. System.out.println(s.substring(s.lastIndexOf(", ") + 2));
dacwe
  • 43,066
  • 12
  • 116
  • 140
36

Perhaps something like:

String s = "abcd,fg;ijkl, cas";
String result = s.substring(s.lastIndexOf(',') + 1).trim();

That is, I take the substring occurring after the last comma and remove surrounding white space afterwards...

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
Mathias Schwarz
  • 7,099
  • 23
  • 28
  • 4
    Mind your case! substring instead of subString – spassvogel Feb 20 '14 at 00:09
  • String s = "C:\\Users\\dataset\\Files\\Upload\\samplejpg.jpg"; String result = s.substring(s.lastIndexOf('\\') + 1).trim(); I used this to extract file name with extension from a path.. – user1767083 Nov 24 '20 at 09:56
15

Always think of looking for the answer for this sort of question in Apache Commons... the basic ones should probably be included with most builds. The most appropriate (not using regexes) is this:

StringUtils.substringAfterLast(String str, String separator)

But if you really want to use a regex for some reason there are a couple of methods which you might want to use, e.g.

String removeAll(String text, String regex)

or

String removeFirst(String text, String regex)

Amusingly, you can get what you want by doing this (relying on greedy quantifiers):

StringUtils.removeFirst( myString, ".*," );

StringUtils is part of apache commons-lang3.

Apart from anything else there is no point re-inventing the wheel. But I can think of at least 2 other advantages:

  1. the Apache people can also be counted on to have developed well tested code dealing with many "gotchas".
  2. these Apache methods are usually well named: you don't have to clutter up your code with stoopid utility methods; instead you have a nice clean method which does what it says on the tin...

PS there's nothing to prevent you looking at the source code to check about what the method actually does. Usually they are very easy to understand.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
4

You could try this:

public static void main(String[] args) {
    String s = " abcd,fg;ijkl, cas";
    String[] words = s.split(",");
    System.out.println(words[words.length-1].trim());
}
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Only one space:

String[] aux = theInputString.split(",\\s");
string result = aux[aux.length-1];

0 to n spaces:

String[] aux = theInputString.split(",\\s*");
string result = aux[aux.length-1];
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
1

What about the following:

String a = "abcd,fg;ijkl, cas";
String[] result = a.split(",[ ]*");
String expectedResult = result[result.length - 1]);
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • I don't know where you found your wisdom, but based on the JavaDocs (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29) and based on my unit Tests it uses Regular expressions!. – khmarbaise Mar 01 '12 at 11:54
0

I used the answers to solve my problem, it just need to extract the file name from path,

String s = "C:\\\\Users\\\\Blue Tag\\\\Files\\\\Upload\\\\samplejpg.jpg"; String result = s.substring(s.lastIndexOf('\\') + 1).trim();

user1767083
  • 157
  • 1
  • 3
  • 10
0

and one more :)

String s = "abcd,fg;ijkl, cas";
String result = s.replaceAll(".*, ", "");
rompetroll
  • 4,781
  • 2
  • 37
  • 50
-1
str.split(",");

then

str.trim()
trapper
  • 11,716
  • 7
  • 38
  • 82