Solved

What do the following reg ex's look like?

Posted on 2006-10-21
8
232 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

624 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