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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:01:30+00:00 2026-05-16T00:01:30+00:00

Using java, I am writting a script to anchor link an html bibliography. That

  • 0

Using java, I am writting a script to anchor link an html bibliography. That is going
from: [1,2]
to: <a href="o100701.html#bib1">[1, 2]</a>

I think I have found the right regex expression: \[.*?\]

What I am having trouble with is writting the code that will retain the values inside the expression while surounding it with the link tags.

This is the most of I can think of

while(myScanner.hasNext())
{
 line = myScanner.nextLine();
 myMatcher = myPattern.matcher(line);
 ...
 outputBufferedWritter.write(line+"\n");
}

The files aren’t very large and there almost always less then 100 matches, so I don’t care about performance.

  • 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-16T00:01:31+00:00Added an answer on May 16, 2026 at 12:01 am

    First of all I think a better pattern to match the [tag] content is [\[\]]* instead of .*? (i.e. anything but opening and closing brackets).

    For the replacement, if the URL varies depending on the [tag] content, then you need an explicit Matcher.find() loop combined with appendReplacement/Tail.

    Here’s an example that sets up a Map<String,String> of the URLs and a Matcher.find() loop for the replacement:

        Map<String,String> hrefs = new HashMap<String,String>();
        hrefs.put("[1,2]", "one-two");
        hrefs.put("[3,4]", "three-four");
        hrefs.put("[5,6]", "five-six");
    
        String text = "p [1,2] \nq [3,4] \nr [5,6] \ns";
    
        Matcher m = Pattern.compile("\\[[^\\[\\]]*\\]").matcher(text);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String section = m.group(0);
            String url = String.format("<a href='%s'>%s</a>",
                hrefs.get(section),
                section
            );
            m.appendReplacement(sb, url);
        }
        m.appendTail(sb);
    
        System.out.println(sb.toString());
    

    This prints:

    p <a href='one-two'>[1,2]</a> 
    q <a href='three-four'>[3,4]</a> 
    r <a href='five-six'>[5,6]</a> 
    s
    

    Note that appendReplacement/Tail do not have StringBuilder overload, so StringBuffer must be used.

    References

    • java.util.regex.Matcher

    Related questions

    • Difference between .*? and .* for regex
    • StringBuilder and StringBuffer in Java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to use some sort of AJAX implementation to… May 16, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer First of all, make sure you're using org-mode 6.36 or… May 16, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer You could as well write for (char *ptr = evil;… May 16, 2026 at 4:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am writing a Java app that uploads image and video files to a
I am currently in the process of writing some software in Java that I
I recently read that Java now sports initialisation blocks like the following: class C
I can persist an object into the DB from Java as such: Table Person:
I have a directory with a name that contains Japanese characters, and I need
I am writing a script for renaming/copying the content of a file. I have
I am writing a Java Agent which makes use of the Java ASM library
I am writing regular expressions for unicode text in Java. However for the particular
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
I'm writing an app in Java in Eclipse where I need to get 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.