0
String str = "FirstName LastName - 1234xx"

In above case, want to replace above string with everything after " - " substring. In the above example it would mean changing str to 1234xx

The length of string after " - " is not fixed, hence cannot just capture last certain no. of characters

This approach gives FirstName LastName - - instead of desired output 1234xx

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "FirstName LastName - 1234xx";
        String newStr = str.replaceAll("(?<=( - )).*", "$1");

        System.out.println(newStr);
    }
}
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew May 26 '20 at 19:01
  • @WiktorStribiżew cannot get correct expression. Any help would be appreciated – Arpit Jain May 26 '20 at 22:44

0 Answers0