• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Replacing string pattern

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
dkim18
Asked:
dkim18
  • 5
1 Solution
 
objectsCommented:
you want to use the replaceAll() method, and use groups for any parts of the matching sting you need to use.

0
 
objectsCommented:
$n is used to insert the nth capturing group.
0
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!

 
objectsCommented:
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
 
dkim18Author Commented:
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
 
objectsCommented:
As long as you achieved your goal :)

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now