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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:59:58+00:00 2026-06-10T23:59:58+00:00

I need a Java code to take this text text1 [example1]http://example1.com [example2]http://example2.com text2 ….

  • 0

I need a Java code to take this text ” text1 [example1]http://example1.com [example2]http://example2.com text2 ….” and return it as the following:

text1 <a target='_blank' href='http://example1.com'>example1</a> <a target='_blank' href='http://example2.com'>example2</a> text2

i mean whenever it finds this pattern [example2]http://example2.com in a text to take and put it in html hyperlink. Please help.. that what i have done till now but its not working

    ViolationTableItem item = new ViolationTableItem(m_violation);
    String dataSource = "";
     String copyDataSource="";
    try {
        dataSource = item.getUi().getViolation().getBlackListEntries()
                .getDataSources();

        Matcher matcher = Pattern.compile(
                "(\\[.*?\\])(.*://[^<>[:space:]]+[[:alnum:]/])").matcher(
                dataSource);

        while (matcher.find()) {

            String matchedLink = matcher.group();

            Matcher nameMatcher = Pattern.compile("\\[.*?\\]").matcher(
                    matchedLink);
            String nameMatched = "";
            String nameMatched2 = "";
            String linkableText = "";
            String[] tst = matchedLink.split("\\[.*?\\]");
            for (int i = 1; i < tst.length; i++) {
                if (nameMatcher.find()) {
                    nameMatched = nameMatcher.group();
                    // System.out.println(nameMatched);
                    nameMatched2 = matchedLink.replace(nameMatched, "");
                    // System.out.println(nameMatched2);

                }
                if (tst[i] != null && !tst[i].equals("")) {
                    linkableText = "<a target='_blank' href='"
                            + tst[i]
                            + "'>"
                            + nameMatched.replaceAll("\\[", "").replaceAll(
                                    "\\]", "") + "</a>";
                    copyDataSource += dataSource.replace(matchedLink,
                            linkableText) + " ";
                }
            }

        }
    } catch (Exception ne){
        return copyDataSource;
    }
    return copyDataSource;

Thanks in advance!

  • 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-11T00:00:00+00:00Added an answer on June 11, 2026 at 12:00 am

    You can do this easily with String.replaceAll():

    String text = " text1 [example1]http://example1.com [example2]http://example2.com text2 ....";
    String link = "<a target='_blank' href='$2'>$1</a>";
    String newText = text.replaceAll("\\[(.*?)\\](.*?)(?=\\s)", link);
    System.out.println(newText);
    

    prints

    text1 <a target='_blank' href='http://example1.com'>example1</a> <a target='_blank' href='http://example2.com'>example2</a> text2 ....
    

    The regex looks for a pair of brackets, captures everything inside and after up until the next space, and replaces it with the link string.
    $1 and $2 are used to find what was captured in the first and second parantheses.

    If you dont want it to match any brackets, but for example just those followed by http://, just change the second (.*?) to (http://.*?).

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

Sidebar

Related Questions

I need the java code snippet for the below logic: Following is a string
Take this Java code: for (ContactsReduced curContact : allcontacts) { .......... allcontacts.remove(curContact); } I
I need a Java code to find the length of a formatted decimal. Eg
I need to debug remote java code with IntelliJ Idea 10.5 (Ultimate Edition) When
I need to ship some Java code that has an associated set of data.
I need to convert a python code into an equivalent java code. Python makes
I have existing java code and need to create Design Document based on that.
I need to play a part of an MP3 file in my java code.
I need to run an external application from within my Java code. I can
I often have a situation in my Java code when I need to set

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.