Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What do the following reg ex's look like?

Posted on 2006-10-21
8
Medium Priority
?
241 Views
Last Modified: 2010-03-31
Hello,
I need to pattern match the following html tags? Please note * means that it could be anything in between.

<head>*</head>
<title>*</title>
<body *>
<table *>


Worth 500 points.

Thanks,
Rick
0
Comment
Question by:richardsimnett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 17782952
As regular expressions are applied to a single line and
your first two examples usually extend over multiple lines
there is no perfect solution to those.

In REGEX a .*  could replace your *

;JOOP!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 17782992
Try something like

      public static String matchTag(String tag, String toMatch) {
            String result = null;
            StringBuilder sb = new StringBuilder("(?ims)").append(tag).insert(tag.length() + 5,"(?: [^>]*)*").append("(.*?)").append(tag).insert(tag.length() + 23, "/");
            Pattern p = Pattern.compile(sb.toString());
            Matcher m = p.matcher(toMatch);
            if (m.find()) {
                  result = m.group(1);
            }
            return result;
      }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17782993
(You would pass the tag as <body>, <title> etc)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:avsrivastava
ID: 17783018
This code will match the longest string(multiple occurrences will show up though).
If you want to match each of the tags, make 4 patterns and then match the input string against each.

 
            Pattern pattern = Pattern.compile("<html>.*</html>|<title>.*</title>|<body .*>|<table .*>");
            Matcher matcher = pattern.matcher(inputString);//inputString is the one in which you want to find the pattern
            boolean found = false;
            while (matcher.find()) {
                System.out.println("I found the text \""+matcher.group()+"\" starting at " +
                   "index "+matcher.start()+" and ending at index "+matcher.end()+"\n");
                found = true;
            }
            if(!found){
                System.out.println("No match found.\n");
0
 

Author Comment

by:richardsimnett
ID: 17783502
Ok guys I see what your saying but couldnt I just do something like this then? (This is what I currently have, but it doesnt work).

html = html.replaceFirst("<head>.*</head>", replaceHead());

Basically the intent is to replace the entire <head> tag and all text contained with it, and replace it with the head generated by replaceHead().  It doesnt seem to ever match. I have also tried it with this variation:

message = message.replaceFirst("/<head>.*</head>/gis", randomHead());


Neither have worked.

Thanks,
Rick
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783541
Have you run the code i posted?
0
 

Author Comment

by:richardsimnett
ID: 17786352
CEHJ,
Yes I just got done testing it... worked great for the <head> tags. Had to change it a little bit to make it do replacements, and I had to make a seperate function to deal with the <table> and <body> tags.. but it also is based on your suggestion.

here are the new functions:

public String replaceTag(String tag, String toMatch,String replacement)
     {
          String result = null;
          StringBuilder sb = new StringBuilder("(?ims)").append(tag).insert(tag.length() + 5,"(?: [^>]*)*").append("(.*?)").append(tag).insert(tag.length() + 23, "/");
          Pattern p = Pattern.compile(sb.toString());
          //cfg.writeLog("Pattern: " + sb.toString());
          Matcher m = p.matcher(toMatch);
          result = m.replaceFirst(replacement);
          return result;
     }
   
     public String replaceTagHead(String tag, String toMatch, String replacement)
     {
         String result = null;
         StringBuilder sb = new StringBuilder("(?ims)").append(tag).insert(tag.length() + 5,"(?: [^>]*)*"); //.append("(.*?)").append(tag).insert(tag.length() + 23, "/");
         Pattern p = Pattern.compile(sb.toString());
         //cfg.writeLog("Tag Head Pattern: " + sb.toString());
         Matcher m = p.matcher(toMatch);
         result = m.replaceFirst(replacement);
         
         return result;
     }

Thanks for the Help!

Rick
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17786730
Well done. At first glance though, the above two methods look the same, and indeed should be the same theoretically, as should any tag replacement you're doing..?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question