Solved

Replacing string pattern

Posted on 2003-12-04
6
413 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

862 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

24 Experts available now in Live!

Get 1:1 Help Now