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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:17:47+00:00 2026-06-07T13:17:47+00:00

I have the following code, which objective is to detect the encoding/charset of a

  • 0

I have the following code, which objective is to detect the encoding/charset of a given webpage, using regular expressions.

I need to test two of the following regex (regexHTML1 and regexHTML2). In this case, the correct regex is the second one, regexHTML2, which outputs:

Found: <meta id="HtmlHead1_desc" name="description" content="Televisores,TV 3D, TV, vídeo e MP3. Compre online Televisores,TV 3D na Fnac" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta
Found: UTF-8

With this code:

URL url = new URL("http://www.fnac.pt/imagem-e-som/TV-3D/Televisores/s21075?bl=HGAChead");
is = url.openStream();

String regexHTML1 = "<meta.*content=\\\".*;.*charset=(.*)\\\"\\s*/?>";
String regexHTML2 = "<meta.*content=\\\".*;.*charset=(.*)\\\"\\s*/?>\\s*<meta";

//  Scanner s = new Scanner(is);        
//  s.findWithinHorizon(regexHTML1, 0);
//  MatchResult result = s.match();
//  for (int i = 0; i <= result.groupCount(); i++)
//      System.out.println("Found: " + result.group(i));
//  s.close();

Scanner s2 = new Scanner(is);
s2.findWithinHorizon(regexHTML2, 0);
MatchResult result2 = s2.match();
for (int i = 0; i <= result2.groupCount(); i++)
    System.out.println("Found: " + result2.group(i));
s2.close();

However, if I uncomment the commented code block that tests the first regex (regexHTML1), the output is:

Found: <meta id="HtmlHead1_desc" name="description" content="Televisores,TV 3D, TV, vídeo e MP3. Compre online Televisores,TV 3D na Fnac" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="PICS-Label" content="(PICS-1.1 &quot;http://www.rsac.org/ratingsv01.html&quot; l gen true comment &quot;RSACi North America Server&quot; by &quot;webmaster@fnac.com&quot; for &quot;http://www.fnac.com/&quot; on &quot;1997.06.30T14:21-0500&quot; r (n 0 s 0 v 0 l 0))" /><link rel="shortcut icon" href="/favicon.ico" /><link id="HtmlHead1_canonicalLink" rel="canonical" href="http://www.fnac.pt/imagem-e-som/TV-3D/Televisores/s21075" />
Found: UTF-8" /><meta http-equiv="PICS-Label" content="(PICS-1.1 &quot;http://www.rsac.org/ratingsv01.html&quot; l gen true comment &quot;RSACi North America Server&quot; by &quot;webmaster@fnac.com&quot; for &quot;http://www.fnac.com/&quot; on &quot;1997.06.30T14:21-0500&quot; r (n 0 s 0 v 0 l 0))" /><link rel="shortcut icon" href="/favicon.ico" /><link id="HtmlHead1_canonicalLink" rel="canonical" href="http://www.fnac.pt/imagem-e-som/TV-3D/Televisores/s21075

Since regexHTML1 is not appropriate. But when testing regexHTML2 (the correct one) it throws an exception:

java.lang.IllegalStateException: No match result available

How is this possible?
The regexHTML2 only works when I’m not testing regexHTML1…

  • 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-07T13:17:49+00:00Added an answer on June 7, 2026 at 1:17 pm

    Input streams are consumed as they are read (i.e., they know their current position). So because you are using the same stream, it gets consumed by the initial scanning operation and nothing is left for the second scanner.

    Use two different streams, or download the entire stream into a String or something similar, and match against that.

    • 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 which works perfect. Objective: Given a number n, find
I have got the following objective-c code which does what I need to do
I have following code which works for radio buttons but need to be changed
I have ubuntu machine and compiling objective-c using GNUStep. I wrote the following code:
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which is fine if I give invalid parameters (though,

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.