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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:27:19+00:00 2026-06-06T13:27:19+00:00

I have the following piece of Java code that reads strings from CSV file.

  • 0

I have the following piece of Java code that reads strings from CSV file. Then, split the strings in order to check them and extract the “anyaddress” part that comes in the pattern:”http://www.anyaddress.anything/“

//Split the file strings since it is CSV file
    while((Line=in.readLine())!=null) 
       strings = Line.split(",");

    for(int i=0; i<strings.length; i++)
    {
        Pattern regex = Pattern.compile(
        "(?<=http://www.)" + "[^/]*", Pattern.COMMENTS);
        Matcher regexMatcher = regex.matcher(strings[i]); 
        if (regexMatcher.find()) 
        { 
           //Returns the input subsequence matched by the previous match.
           ResultString = regexMatcher.group();                
           out.write(ResultString);
           out.newLine();
        }  //end if

    } //end for loop

    in.close();
    out.close();

Now, I found that my text file might contains strings in the following different formats:
‘http://www.anyaddress.anything/‘ OR ‘http://anyaddress.anything/‘ OR ‘https://www.anyaddress.anything/‘ OR ‘https://anyaddress.anything/‘

I need to extract the “anyaddress” part only.I have searched in previous posts can we check multiple patterns using regex in java? and found that I only have to add “|”. But for example, when I edited my regex to include the second pattern by adding:

Pattern regex = Pattern.compile(
        "(?<=http://www.) | (?<=http://)" + "[^/]*", Pattern.COMMENTS);

my program extracted the addresses as: http://www.anyaddress.anything, while I only need the “anyaddress.anything/” part only, in addition to that the program correctly extract the addresses that does not have the “www.” which was not able to extract previously.

Can anybody clarify to me where is my mistake? and give me example how can I include multiple patterns to make my program extract the links in any of the 4 mentioned format correctly ?

  • 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-06T13:27:21+00:00Added an answer on June 6, 2026 at 1:27 pm

    I would avoid lookback because it is infrequently used and not necessary here. Also, I don’t know how it combines with alternation. Since you are parsing URLs I would recommend using the URL or URI class, extracting the domain name and then removing any leading ‘www’. If you still want to use regexes, try

    Pattern.compile("https?//:(?:www[.])?([^/]*)")
    

    That reads

    http, plus an optional ‘s’ slash slash colon an optional ‘www.’ and a capture group of everything up to (but excluding) the next slash

    And you read the result using group(1) because it is the first capture group, not the whole match.

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

Sidebar

Related Questions

I have the following piece of code that is taken from a mock exam
I have the following piece of pseudo-C/Java/C# code: int a[]= { 30, 20 };
I have the following piece of code that takes in some words, stores them
A newbie question. I have the following piece of Java code: import acm.program.*; import
I have following piece of code in my jsp file which getSession information: <jsp:useBean
I have the following piece of code in a java application Thread.currentThread().sleep(10000); However eclipse
I have following piece of code from my Backbone project: App.Controllers.Test = Backbone.Router.extend({ routes:
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code in my Model: public function getSite() {

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.