Solved

What do the following reg ex's look like?

Posted on 2006-10-21
8
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
(You would pass the tag as <body>, <title> etc)
0
 
LVL 2

Expert Comment

by:avsrivastava
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:richardsimnett
Comment Utility
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
Comment Utility
Have you run the code i posted?
0
 

Author Comment

by:richardsimnett
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now