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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:16+00:00 2026-05-26T01:54:16+00:00

I’m quite new to Java and I’m facing a situation I can’t solve. I

  • 0

I’m quite new to Java and I’m facing a situation I can’t solve. I have some html code and I’m trying to run a regular expression to store all matches into an array. Here’s my code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.regex.PatternSyntaxException;

    public class RegexMatch{
      boolean foundMatch = false;
      public String[] arrayResults;

      public String[] TestRegularExpression(String sourceCode, String pattern){
      try{
      Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
      Matcher regexMatcher = regex.matcher(sourceCode);
      while (regexMatcher.find()) {
        arrayResults[matches] = regexMatcher.group();
        matches ++;
      } 
    } catch (PatternSyntaxException ex) {
      // Exception occurred
    }
    return arrayResults;
  }
}

I’m passing a string containing html code and the regular expression pattern to extract all meta tags and store them into the array. Here’s how I instantiate the method:

RegexMatch regex = new RegexMatch();
regex.TestRegularExpression(sourceCode, "<meta.*?>");
String[] META_TAGS = regex.arrayResults;

Any hint?
Thanks!

  • 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-26T01:54:17+00:00Added an answer on May 26, 2026 at 1:54 am

    Firstly, parsing HTML with regular expressions is a bad idea. There are alternatives which will convert the HTML into a DOM etc – you should look into those.

    Assuming you still want the “match multiple results” idea though, it seems to me that a List<E> of some form would be more useful, so you don’t need to know the size up-front. You can also build that in the method itself, rather than having state. For example:

    import java.util.*;
    import java.util.regex.*;
    
    public class Test
    {
        public static void main(String[] args)
            throws PatternSyntaxException
        {
            // Want to get x10 and x5 from this
            String text = "x10 y5 x5 xyz";
            String pattern = "x\\d+";
            List<String> matches = getAllMatches(text, pattern);
            for (String match : matches) {
                System.out.println(match);
            }
        }
    
        public static List<String> getAllMatches(String text, String pattern)
            throws PatternSyntaxException
        {
            Pattern regex = Pattern.compile(pattern);
            List<String> results = new ArrayList<String>();
            Matcher regexMatcher = regex.matcher(text);
            while (regexMatcher.find()) {
                results.add(regexMatcher.group());
            }
            return results;
        }
    }
    

    It’s possible that there’s something similar to this within the Matcher class itself, but I can’t immediately see it…

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.