I'm trying to extract a string which exists between a word and a new line character in a base string. For example if the base string is:
String s = "Info\n"
+ "user id: 1234\n"
+ "Region: us-east-1";
So I want to extract the String between user id
and the very next new line character (\n
) to it. Extracted string should return: 1234
How can I use a regex
pattern to achieve this? Any help would be appreciated.
I tried this to extract using substring but did not succeed.
str.substring(str.indexOf("user id:") + 1, str.indexOf("\n"));