Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:59:55+00:00 2026-05-11T16:59:55+00:00

I need to replace all & in a String that isnt part of a

  • 0

I need to replace all & in a String that isnt part of a HTML entity. So that the String “This & entites > & <” will return “This & entites > & <“

And I’ve come up with this regex-pattern: “&[a-zA-Z0-9]{2,7};” which works fine.
But I’m not very skilled in regex, and when I test the speed over 100k iterations, it uses double amount of time over a previous used method, that didnt use regex. (But werent working 100% either).

Testcode:

long time = System.currentTimeMillis();
String reg = "&(?!&#?[a-zA-Z0-9]{2,7};)";
String s="a regex test 1 & 2  1&2 and &_gt; - &_lt;"
for (int i = 0; i < 100000; i++) {test=s.replaceAll(reg, "&amp;");}
System.out.println("Finished in:" + (System.currentTimeMillis() - time) + " milliseconds");

So the question would be whether there is some obvious ways of optimize this regex expression for it to be more effective?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:59:55+00:00Added an answer on May 11, 2026 at 4:59 pm

    s.replaceAll(reg, "&amp;") is compiling the regular expression every time. Compiling the pattern once will provide some increase in performance (~30% in this case).

    long time = System.currentTimeMillis();
    String reg = "&(?!&#?[a-zA-Z0-9]{2,7};)";
    Pattern p = Pattern.compile(reg);
    String s="a regex test 1 & 2  1&2 and &_gt; - &_lt;";
    for (int i = 0; i < 100000; i++) {
        String test = p.matcher(s).replaceAll("&amp;");
    }
    System.out.println("Finished in:" + 
                 (System.currentTimeMillis() - time) + " milliseconds");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string. I need to replace all instances of a given array
I need help to replace all \n (new line) caracters for in a String,
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I need to replace all special control character in a string in Java. I
Say I have a string that looks like this: str = The &yquick &cbrown
I need to replace all WinAPI calls of the CreateFile, ReadFile, SetFilePointer, CloseHandle with
I need to replace one of the pre-defined window classes all across Windows. For
I need to replace character (say) x with character (say) P in a string,
I need to send HTML emails directly from oracle PL/SQL package. This works almost
We need to replace the menu system in our main ASP.NET application. So naturally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.