Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaped characters get "un-escaped" when using Matcher.appendReplacement #18

Closed
vprcic opened this issue Sep 6, 2022 · 4 comments · Fixed by #20
Closed

Escaped characters get "un-escaped" when using Matcher.appendReplacement #18

vprcic opened this issue Sep 6, 2022 · 4 comments · Fixed by #20

Comments

@vprcic
Copy link

vprcic commented Sep 6, 2022

I don't know whether this is still maintained, but there's a bug when using the Matcher.appendReplacement that removes escaping of characters like "\s". Here's a code example:

Pattern pattern = Pattern.compile("[0-9]");
Matcher matcher = pattern.matcher("12345");
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(buffer, matcher.group() + "\\s");
}
matcher.appendTail(buffer);
System.out.println(buffer);

This should print "1\s2\s3\s4\s5\s" but prints "1s2s3s4s5s".

@tony19
Copy link
Owner

tony19 commented Sep 6, 2022

Thanks for the report. I'll take a look as soon as I can.

@vprcic
Copy link
Author

vprcic commented Sep 6, 2022

Thank you very much!

@vprcic
Copy link
Author

vprcic commented Sep 6, 2022

After some investigation, this seems to also be a problem with regular Matcher class and is resolved using the Matcher.quoteReplacement method. Here's the fixed code example from above:

Pattern pattern = Pattern.compile("[0-9]");
Matcher matcher = pattern.matcher("12345");
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(buffer, Matcher.quoteReplacement(matcher.group() + "\\s"));
}
matcher.appendTail(buffer);
System.out.println(buffer);

This prints "1\s2\s3\s4\s5\s" as expected. So, maybe to resolve this issue, all that is needed is to expose the static Matcher.quoteReplacement method in the Matcher class?

@tony19
Copy link
Owner

tony19 commented Sep 8, 2022

Matcher.quoteReplacement released in https://github.com/tony19/named-regexp/releases/tag/named-regexp-0.2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants