1

I have a text while which shows certain output in the form of strings and numbers and from that I have a specific piece of text as follows .I want to extract a certain piece of text from that

This is a small piece of my content

gjhsgdjhgfjhdgfjkhsdgfjkhgsdfk7868736876534
Pattern is:
(4,24,[5,96]) -> (1,14,[14,15]), that is: "Poland" -> "Poland"
(4,46,[36,39]) -> (1,14,[14,15]), that is: "Kim" -> "Kim"

fhafsdhjdfgjhdagfkhj66876493985694(hjiikfghkjfhg,98698)
(hhgjhgdfj,jhgkjdhgf)

From this I want to get the output as

"Poland" -> "Poland"
"Kim" -> "Kim"
"The police" -> "The city police on Friday"

The code I tried is

Pattern p = Pattern.compile("(Pattern is:)\\((.*?)\\)");
  Matcher m = p.matcher(str);

  while(m.find())
  {
      System.out.println("fhkjsdfhkjsdh");
      System.out.println(m.group(2));

  }
6
  • @user3580294 "Pattern is:" is the piece of text I want to extract..My text file contains the word"Pattern is" and I want to extract the content after that Commented May 18, 2014 at 5:37
  • 1
    Where did on Friday go? Commented May 18, 2014 at 5:38
  • @UlugbekUmirov sry my mistake. I had editted my ques Commented May 18, 2014 at 5:39
  • Is your input spanning in multiple lines as shown? Commented May 18, 2014 at 5:40
  • @chopu Ah, must have missed that earlier. My mistake. Commented May 18, 2014 at 5:41

2 Answers 2

1

You can use this:

Pattern p = Pattern.compile("\"[^\"]+\"\\s*->\\s*\"[^\"]+\"");

Use the overall match.

See demo

The expression just checks for strings of the form "Poland" -> "Poland" without bothering to check that Pattern is is matched anywhere, because (i) in the input that does not seem needed, and (ii) it is unclear what would constitute an ending delimiter of a Pattern section.

Let us know if this does what you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Glad it works chopu, thanks for letting me know. By the way this is not hwnd, though I'm sure he would have produced a great answer. :)
oh sry zx81 ..my mistake
@chopu no worries, it's a compliment. :)
0

try this :

String newStr = str.replaceAll("^.*:", "");
System.out.println(newStr);

1 Comment

thank you for answering and giving attention to my question. But it's not the output I needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.