2

I have a code that looks for Regex matches and adds a new line after the regex Match. This works on the first click of a button, but when I click it again, it still captures the regex.

Example Scenario:

Change all sentences ending with 'dog.' and add a new line if it is not the end of the line.

Sample code inside mouse click event:

Pattern pattern = Pattern.compile("dog\\.(?!\n)");
Matcher matcher = pattern.matcher(paragraph);
paragraph = matcher.replaceAll("dog\\.\n");

Sample Input:

I found a street animal. It was a dog. Then the dog found another dog. It was kind of fun to see the two dogs barking to each other.

First click of button that implements code:

I found a street animal. It was a dog. 
Then the dog found another dog. 
It was kind of fun to see the two dogs barking to each other.

Second click of button that implements code:

I found a street animal. It was a dog. 

Then the dog found another dog. 

It was kind of fun to see the two dogs barking to each other.

I'm quite confused on why on the second click, it fails to notice that there is no more instance of 'dog.' without a new line.

Thanks in advance for the help!

6
  • 1
    Use Pattern pattern = Pattern.compile("dog\\.(?!\\n)"); Commented Jun 13, 2016 at 5:05
  • Hi @anubhava, I tried it but results are still the same. Commented Jun 13, 2016 at 5:08
  • Maybe (?!\\r\\n) works, if the files uses CRLF Commented Jun 13, 2016 at 5:11
  • @SebastianProske Hi, the text is only inputted during runtime through SWT (Window Builder) textbox. Commented Jun 13, 2016 at 5:14
  • I have tried the code with string and it works as expected (on Windows). What is your OS? MacOS? Maybe SWT converts your \n to \r. Then this pattern should work dog\\.(?![\\n\\r]) Commented Jun 13, 2016 at 5:38

2 Answers 2

1

You can use:

String str = 
"I found a street animal. It was a dog. \nThen the dog found another dog. \nIt was kind of fun to see the two dogs barking to each other."; 

Pattern pattern = Pattern.compile("(?<=dog\\.(?!\\s*\\n))\\s*");
Matcher matcher = pattern.matcher(str);
str = matcher.replaceAll("\r\n");

RegEx Demo

Code Demo

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

4 Comments

Hi, this code works. However, it does not work once I change the input string. Note that I am working with SWT, and I have a textbox where user can input paragraph and button that does the check. For example: I input the sample text. Clicked the button and it works, but when I append new lines to the first text and clicked the button again, it does not work and it removes all the spaces in the newly added text.
Hi, yes it does work if it's running only once. I have SWT Window Builder and I can change the text anytime during runtime. I have a textbox where user can input text. If user inputs text and clicks the button, it works but when user adds more text in the texbox and clicks the button, it doesnt work anymore.
Hi @anubhava , I have checked and it works fine when I print the result in console but it is not showing correctly in SWT textbox. Thanks.
Hi @anubhava, I found it out. SWT's new line is \r\n. That's why it only works once. Thanks!
0

hope below code might help you..

    String paragraph = "I found a street animal. It was a dog. Then the dog found another dog. It was kind of fun to see the two dogs barking to each other.";
    paragraph = paragraph.replaceAll("dog\\.(?!\n)", "dog\\.\n");

thnx.

2 Comments

Your solution is to switch from Matcher's replaceAll() method to String's replaceAll() method? How does that help?
But that's all it does. Your suggestion is exactly the same as what the OP is already doing, only in one line instead of three. An answer should attempt to solve the problem posed in the question, and this one doesn't.

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.