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 7780667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:58:26+00:00 2026-06-01T18:58:26+00:00

I have a string where I have to replace some content: …content… <a href=’document/link/B1′>foo</a>

  • 0

I have a string where I have to replace some content:

"...content... <a href='document/link/B1'>foo</a> ...content... <a href='document/link/B2'>bar</a> ..."

I’m looking for a clean way to obtain something like this:

"...content... <a href='document/link/23'>foo</a> ...content... <a href='document/link/24'>bar</a> ..."

Where ’23’ and ’24’ in the links are results of some processing that I did. So first I should be able to select the links, get their url (more specific: I need the B1 and B2) and then I have to perform some actions with e.g. B1 which results in ’23’ which I then have to insert back again in the string.

Is there a nice way to achieve this?

  • 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-01T18:58:29+00:00Added an answer on June 1, 2026 at 6:58 pm

    In general, it’s bad idea to use regex to parse HTML/XML. But for some sporadical use (run just once) and if you are sure about the structure of your HTML and don’t require much robustness, something like this (based on this) could do the trick:

       String original = "..content... <a href='document/link/B1'>foo</a> ...content... <a href='document/link/B2'>bar</a> ...";
       StringBuffer sb = new StringBuffer();
       // tweak the following
       Pattern pattern = Pattern.compile("(<a href='document/link/)([^']*)('>)");
       Matcher matcher = pattern.matcher(original);
       while(matcher.find()) {
          String oldlinkPart = matcher.group(2);
          String newlinkPart = buildNewLinkPart(oldLinkPart); // here you do your look-up
          matcher.appendReplacement(sb, matcher.group(1) + newlinkPart + matcher.group(3));
       }
       matcher.appendTail(sb);
       String modified = sb.toString();
    

    You can tweak the regex pattern to be slightly more general (more spaces, tabs, additional atttributes inside the A tag, case sensitivity, doble quotes), but when you start to pretend to be totally general, so that your code works with any well formed HTML, then you’re screwed: try instead with a XML/DOM parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string looking like: some+thing+-+More How do i replace the + sign?
I have a string containing HTML and I need to replace some words to
HI, I have some string from XML file, and I I want to replace
I have a string like this BRADI5G20430|BRADI5G20430.1||1 How can I replace the bar (single
if i have a string like <p>this is some content</p><script>alert('hello');</script> i want to get
I have a string (partly HTML) where I want to replace the string :-)
I have this string aa b qqidjwljd p fjem I need to replace b
goal: I have the string 1234432144 I want to only replace the first 2
assume I have a string 1,2,3,4 Now I want to replace, e.g. the 3rd
I have this string: var someString = 23/03/2012; and want to replace all the

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.