0

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"));
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
  • 1
    When doing the operation in a single line you retrieve the index of the first end line (which is after info). Instead, if you use two instructions you can retrieve the first endline of the string after removing everything before user id. Try: `str = str.substring(str.indexOf("user id:") + 1); str = str.substring(0, str.indexOf("\n"))` – Cristian Ramon-Cortes Nov 06 '19 at 14:54
  • 1
    Why regex? How about iterating over all lines, check if line starts with `"user id: "`, if yes print rest of it? Something like `Scanner sc = new Scanner(yourText); while(sc.hasNextLine){String line = sc.nextLine(); if (line.startsWith("user id:"){ String data = line.substring("user id:".length()).trim(); ` here you have data you ware looking for. – Pshemo Nov 06 '19 at 14:54
  • @CristianRamon-Cortes i'll try that approach and see. – Kulasangar Nov 06 '19 at 14:56
  • BTW last line will probably not have `\n` after it which may cause some problems with `indexOf("\n")`. – Pshemo Nov 06 '19 at 14:56
  • @Pshemo i'm literally hard coding the \n here, where as in the actual scenario there'll be a line break in the base string. – Kulasangar Nov 06 '19 at 14:57
  • 1
    @CristianRamon-Cortes This returns "ser id: 1234" for me. – yur Nov 06 '19 at 14:57
  • ```String result = str.substring(str.indexOf(":") + 1, str.indexOf("R")); while(Character.isWhitespace(result.charAt(result.length() - 1))){result = result.substring(0, text.length() - 1);} while(Character.isWhitespace(result.charAt(0))){result= result.substring(1, result.length());}``` – yur Nov 06 '19 at 15:00
  • @OP assuming that your String always procedes "user id:" and percedes "Region", this code will work. The two whiles are there to remove Whitespace from the front and end of the String. – yur Nov 06 '19 at 15:02
  • @Kulasangar You need to first match the "user id:" and get the substring removing the characters of "user id". Then you need to remove everything after the end line. Try this code: `String s = "Info\n" + "user id: 1234\n" + "Region: us-east-1"; String tag = "user id: "; s = s.substring(s.indexOf(tag) + tag.length()); s = s.substring(0, s.indexOf('\n'));` – Cristian Ramon-Cortes Nov 06 '19 at 15:03
  • 2
    Honestly kind of annoying that this was closed while multiple people were willing to help him come up with the specific regex to be used here. I agree that it's a duplicate, but still people were willing to help for the moment and now we have to answer with these terribly formatted code comments. – yur Nov 06 '19 at 15:05
  • @yur thanks for the support. Yeah i was quite new to regex and was trying multiple things. But couldn't make it work. That's why I ended up asking it here. – Kulasangar Nov 06 '19 at 15:06
  • @CristianRamon-Cortes alright sure. will give it a go now and get back. – Kulasangar Nov 06 '19 at 15:07
  • 1
    I totally agree with @yur . The solutions provided on the related post are generic and for experienced users while this question was much more specific. Closing it only makes harder to help. – Cristian Ramon-Cortes Nov 06 '19 at 15:09
  • @CristianRamon-Cortes worked for me. :) – Kulasangar Nov 06 '19 at 15:10
  • @CristianRamon-Cortes thanks. – Kulasangar Nov 06 '19 at 15:11
  • 1
    @yur thanks a bunch. – Kulasangar Nov 06 '19 at 15:11

0 Answers0