Solved

Replacing string pattern

Posted on 2003-12-04
6
416 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 92

Accepted Solution

by:
objects earned 350 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 68
maven module vs maven project 3 74
Java basic valueOf question 1 34
dao vs facade design patterns 2 36
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 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…
This video teaches viewers about errors in exception handling.

733 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