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.
var s = 'test, test, test'; s = s. replace(/,([^,]*)$/, 'and $1'); This will find the last comma in a string and replace it with "and" and anything that came after it.
The substringAfterLast() method is a static method of StringUtils . It is used to return the substring that comes after the last occurrence of the given separator. The separator is not returned along with the substring.
Java – Split a String with Specific Character To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
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:
System.out.println(s.substring(s.lastIndexOf(",") + 1).trim());System.out.println(s.substring(s.lastIndexOf(", ") + 2));If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With