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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:17:46+00:00 2026-05-11T22:17:46+00:00

i am trying to find a certain tag in a html-page with java. all

  • 0

i am trying to find a certain tag in a html-page with java. all i know is what kind of tag (div, span …) and the id … i dunno how it looks, how many whitespaces are where or what else is in the tag … so i thought about using pattern matching and i have the following code:

 // <tag[any character may be there or not]id="myid"[any character may be there or not]>
 String str1 = "<" + Tag + "[.*]" + "id=\"" + search + "\"[.*]>";
 // <tag[any character may be there or not]id="myid"[any character may be there or not]/>
 String str2 = "<" + Tag + "[.*]" + "id=\"" + search + "\"[.*]/>";
 Pattern p1 = Pattern.compile( str1 );
 Pattern p2 = Pattern.compile( str2 );
 Matcher m1 = p1.matcher( content );
 Matcher m2 = p2.matcher( content );
 int start = -1;
 int stop = -1;
 String Anfangsmarkierung = null;
 int whichMatch = -1;

 while( m1.find() == true || m2.find() == true ){

        if( m1.find() ){
            //System.out.println( " ... " + m1.group() );
            start = m1.start();
            //ende = m1.end();
            stop = content.indexOf( "<", start );
            whichMatch = 1;
        }
        else{
            //System.out.println( " ... " + m2.group() );
            start = m2.start();
            stop = m2.end();
            whichMatch = 2;
        }
 }

but i get an exception with m1(m2).start(), when i enter the actual tag without the [.*] and i dun get anything when i enter the regular expression 🙁 … i really havent found an explanation for this … i havent worked with pattern or match at all yet, so i am a little lost and havent found anything so far. would be awesome if anyone could explain me what i am doing wrong or how i can do it better …

thnx in advance 🙂

… dg

  • 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-11T22:17:46+00:00Added an answer on May 11, 2026 at 10:17 pm

    Here is an example for what you’re trying to do adapted from one of my notes:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
    
        public static void main(String[] args) {
    
            String tag = "thetag";
            String id = "foo";
    
            String content = "<tag1>\n"+
                    "<thetag name=\"Tag Name\" id=\"foo\">Some text</thetag>\n" +
                    "<thetag name=\"AnotherTag\" id=\"foo\">Some more text</thetag>\n" +
                    "</tag1>";
    
            String patternString = "<" + tag + ".*?name=\"(.*?)\".*?id=\"" + id + "\".*?>";
    
            System.out.println("Content:\n" + content);
            System.out.println("Pattern: " + patternString);
    
            Pattern pattern = Pattern.compile(patternString);
    
            Matcher matcher = pattern.matcher(content);
    
            boolean found = false;
            while (matcher.find()) {
                System.out.format("I found the text \"%s\" starting at " +
                        "index %d and ending at index %d.%n",
                        matcher.group(), matcher.start(), matcher.end());
                System.out.println("Name: " + matcher.group(1));
                found = true;
            }
            if (!found) {
                System.out.println("No match found.");
            }
        }
    }
    

    You’ll notice that the pattern string becomes something like <thetag.*?name="(.*?)".*?id="foo".*?> which will search for tags named thetag where the id attribute is set to “foo”.

    Note the following:

    • It uses .*? to weakly match zero or more of anything (if you don’t understand, try removing the ? to see what I mean).
    • It uses a submatch expression between parenthesis (the name="(.*?)" part) to extract the contents of the name attribute (as an example).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I assume you have already looked into Asynchronous ASP.NET page… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer The Opacity property only exists on the form so there's… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer Readability aside, the compiler should be smart enough to generate… May 11, 2026 at 11:31 pm

Related Questions

I'm trying to use XML documentation on the members of a class, and in
I have a set of changes which work perfectly against a certain linux version,
I am trying to find a better way to integrate IRB with my normal
I need to check if a certain value SheetNumber is in my mysql database

Trending Tags

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

Top Members

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.