Solved

What do the following reg ex's look like?

Posted on 2006-10-21
8
218 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
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 500 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

789 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