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

  • SEARCH
  • Home
  • 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 8262917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:55:15+00:00 2026-06-08T03:55:15+00:00

I have the following code which should remove all HTML from a part of

  • 0

I have the following code which should remove all HTML from a part of string, which is quoted by dollar signs (could be more of them). This works fine, but I also need to preserve those dollar signs. Any suggestions, thanks

private static String removeMarkupBetweenDollars(String input){
    if ((input.length()-input.replaceAll("\\$","").length())%2!=0)
    {
        throw new RuntimeException("Missing or extra: dollar");
    }
    Pattern pattern = Pattern.compile("\\$(.*?)\\$",Pattern.DOTALL);
    Matcher matcher = pattern.matcher(input);

    StringBuffer sb =new StringBuffer();

    while(matcher.find())
         { //prepending does NOT work, if sth. is in front of first dollar
        matcher.appendReplacement(sb,matcher.group(1).replaceAll("\\<.*?\\>", ""));
        sb.append("$"); //note this manual appending
    }
    matcher.appendTail(sb);
    System.out.println(sb.toString());

    return sb.toString();
}

Thanks for help!

        String input="<p>$<em>something</em>$</p>  <p>anything else</p>";
    String output="<p>$something$</p>  <p>anything else</p>";

More complicated input and output:

String input="<p>$ bar  <b>foo</b>  bar <span style=\"text-decoration: underline;\">foo</span>  $</p><p>another foos</p> $ foo bar <em>bar</em>$";
String output="<p>$ bar  foo  bar foo  $</p><p>another foos</p> $ foo bar bar$"
  • 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-06-08T03:55:18+00:00Added an answer on June 8, 2026 at 3:55 am

    Just some minor tweaks to your code:

    private static String removeMarkupBetweenDollars(String input) {
        if ((input.length() - input.replaceAll("\\$", "").length()) % 2 != 0) {
            throw new RuntimeException("Missing or extra: dollar");
        }
    
        Pattern pattern = Pattern.compile("\\$(.*?)\\$", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(input);
    
        StringBuffer sb = new StringBuffer();
    
        while (matcher.find()) {
            String s = matcher.group().replaceAll("<[^>]+>", "");
            matcher.appendReplacement(sb, Matcher.quoteReplacement(s));
        }
        matcher.appendTail(sb);
    
        return sb.toString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which should be getting the text from a span
I have the following code which should put programs startable in Bash. if [
I have following code: os.chdir(os.path.dirname(os.path.realpath(__file__)) + /../test) path.append(os.getcwd()) os.chdir(os.path.dirname(os.path.realpath(__file__))) Which should add /../test to
I have the following lines of php code which should rename a file which
I have the following code, which should adds a field to a form if
This should be a pretty straightforward question. I have the following code, which forms
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can

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.