javaregexstring

Java pattern matching using regex


I am new to java coding and using pattern matching.I am reading this string from file. So, this will give compilation error. I have a string as follows :

String str = "find(\"128.210.16.48\",\"Hello Everyone\")" ; // no compile error

I want to extract "128.210.16.48" value and "Hello Everyone" from above string. This values are not constant.

can you please give me some suggestions? Thanks


Solution

  • Try with String.split()

    String str = "find(\"128.210.16.48\",\"Hello Everyone\")" ;
    System.out.println(str.split(",")[0].split("\"")[1]);
    System.out.println(str.split(",")[1].split("\"")[1]);
    

    Output:

    128.210.16.48
    Hello Everyone
    

    Edit: Explanation:

    For the first string split it by comma (,). From that array choose the first string as str.split(",")[0] split the string again with doublequote (") as split("\"")[1] and choose the second element from the array. Same the second string is also done.