?
Solved

Replacing string pattern

Posted on 2003-12-04
6
Medium Priority
?
418 Views
Last Modified: 2010-08-05
I am trying to accomplish the following:

In the downloaded HTML source at <download dir>/<filename>.html replace the src attribute of all img tags with relative links to the downloaded image files.
So, for example, the index.html original <img src="hw5/CheckOut.gif"...> should be replaced with the relative <img src="index_html_files/CheckOut.gif"...>. You may assume that all image tags are of the form <img...src="<linked image>"...>. Image tags may also contain alt, width and height attributes in any order (i.e. src attribute could be first last or in between but You really do not care about the other attributes). The src attribute MAY NOT contain any .. relative paths!

And this is what I did so far:
----------------------------
  public static String patternReplace(String htmlWebPage, String subDirName){
    final int FLAGS = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL ;
    final String REPLACE_PATTERN = "<img\\s+src\\s*=\\s*('|\")(.*?)('|\")";

    Pattern myPattern = Pattern.compile(REPLACE_PATTERN, FLAGS);
    Matcher myMatcher = myPattern.matcher(htmlWebPage);

    StringBuffer buffy = new StringBuffer();
    if(myMatcher.find() ){
      myMatcher.appendReplacement(buffy, subDirName);
    }

    myMatcher.appendTail(buffy);
    System.out.println(buffy.toString());

    String newHtml=buffy.toString();
    return newHtml;
  }
-----------------
final String REPLACE_PATTERN = "<img\\s+src\\s*=\\s*('|\")(.*?)('|\")";
this will find anything with the above pattern, but how do I replace image directory with subDirName that I am passing in?
I am thinking I might have to use Groups, but i don't have much idea how to do it.

0
Comment
Question by:dkim18
[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
  • 5
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9879361
you want to use the replaceAll() method, and use groups for any parts of the matching sting you need to use.

0
 
LVL 92

Expert Comment

by:objects
ID: 9879387
$n is used to insert the nth capturing group.
0
 
LVL 92

Expert Comment

by:objects
ID: 9879396
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Accepted Solution

by:
objects earned 1400 total points
ID: 9879511
so you'll need to change your regexp to break the src path into path and filename, and replace it with the following (where n is the group number of the filename):

"<img src=\"index_html_files/$n\""
0
 

Author Comment

by:dkim18
ID: 9880319
I guess I used a little trick without using group and replaceAll()

  public static String patternReplace(String htmlWebPage, String subDirName, String[] counter){
    final int FLAGS = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL ;
    final String REPLACE_PATTERN = "<img\\s+src\\s*=\\s*('|\")(.*?)/";
    String replace_str = "<img src=\"" + subDirName + "/";
    Pattern myPattern = Pattern.compile(REPLACE_PATTERN, FLAGS);
    Matcher myMatcher = myPattern.matcher(htmlWebPage);

    StringBuffer buffy = new StringBuffer();
    for(int i = 0; i < counter.length ; i++){
      if (myMatcher.find()) {
        myMatcher.appendReplacement(buffy, replace_str);
      }
    }

    myMatcher.appendTail(buffy);

It couldn't get the all the concepts, so this is fine for now.
Thanks anyway...
0
 
LVL 92

Expert Comment

by:objects
ID: 9880346
As long as you achieved your goal :)

http://www.objects.com.au/staff/mick
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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