Skip to content

Refactor to reuse java.util.regex.Pattern #25

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

Merged
merged 1 commit into from
Sep 13, 2018
Merged

Refactor to reuse java.util.regex.Pattern #25

merged 1 commit into from
Sep 13, 2018

Conversation

benelog
Copy link
Contributor

@benelog benelog commented Sep 13, 2018

String.replaceAll () compiles the regular expression each time it is called.

   public String replaceAll(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }

It is suggested in javadoc that it is efficient to reuse the pattern, so I modified it accordingly.

If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

@wumpz
Copy link
Collaborator

wumpz commented Sep 13, 2018

You are right.

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 this pull request may close these issues.

2 participants