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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:36+00:00 2026-05-30T13:56:36+00:00

I have the following code private String anchorRegex = \\<\\s*?a\\s+.*?href\\s*?=\\s*?([^\\s]*?).*?\\>; private Pattern anchorPattern =

  • 0

I have the following code

private String anchorRegex = "\\<\\s*?a\\s+.*?href\\s*?=\\s*?([^\\s]*?).*?\\>";
private Pattern anchorPattern = Pattern.compile(anchorRegex, Pattern.CASE_INSENSITIVE);
String content = getContentAsString();
Matcher matcher = anchorPattern.matcher(content);

while(matcher.find()) {
    System.out.println(matcher.group(1));
}

The call to getContentAsString() returns the HTML content from a web page. The problem I’m having is that the only thing that gets printed in my System.out is a space. Can anyone see what’s wrong with my regex?

Regex drives me crazy sometimes.

  • 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-30T13:56:37+00:00Added an answer on May 30, 2026 at 1:56 pm

    You need to delimit your capturing group from the following .*?. There’s probably double quotes " around the href, so use those:

    <\s*a\s+.*?href\s*=\s*"(\S*?)".*?>
    

    Your regex contains:

    ([^\s]*?).*?
    

    The ([^\s]*?) says to reluctantly find all non-whitespace characters and save them in a group. But the reluctant *? depends on the next part, which is .; any character. So the matching of the href aborts at the first possible chance and it is the .*? which matches the rest of the URL.

    • 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: private static final Set<String> allowedParameters; static { Set<String> tmpSet
I have the following code: private ActionResult Foo(string format, IEnumerable<String> myResults) { if (format
I have the following code: private void SetControlNumbers() { string controlString = ; int
I have the following piece of code: private String toString(List<DrugStrength> aDrugStrengthList) { StringBuilder str
Hi I have some strings generated using the following code: private static string CalcHashCode(byte[]
I am trying to compile the following code: private String dataToString(){ Map data =
I have the following code: private void Process(string path) { using (FileStream fs =
I have the following code - private static void convert() { string csv =
I have the following code: [DataContract] class TestContract { private String _Name; private Int32
I have the following code: private bool IsMousetrapFile(string path) { logger.Log(validateFileMessage + path); Excel.Application

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.