double search and replace

Discussion in 'Java' started by text news, Aug 22, 2003.

  1. text news

    text news Guest

    I have already posted a little earlier today "Extract text to html href" and
    please forgive this second post but I have given it some thought and this
    may be a little more focused.
    It is the same problem but maybe a much simpler approach.

    I need to do an advanced search and replace on a large number of files.
    The search string is easy and identical in every file but the replace string
    will be different on every file, although it can be easily identified and
    exists in each file.

    For example:

    text of file 1.

    "photo1
    blah blah blah blah
    xxxxx"

    text of file 2.

    "photo2
    blah blah blah blah
    xxxxx"

    text of file 3.

    "photo3
    blah blah blah blah
    xxxxx"

    I want to replace xxxxx in each case with photo1, photo2 etc.

    I could do this with wild cards but need a kind of double search.
    i.e. search for photo# and make this the replace string.
    now search for xxxxx and replace it with the result of the first search.

    That must make more sense than my earlier posting.

    Thanks
    Les
     
    text news, Aug 22, 2003
    #1
    1. Advertising

  2. text news

    Roedy Green Guest

    On Fri, 22 Aug 2003 22:18:47 +0100, "text news"
    <> wrote or quoted :

    >I have already posted a little earlier today "Extract text to html href" and
    >please forgive this second post but I have given it some thought and this
    >may be a little more focused.


    A couple of thinks you put in your pipe to smoke:

    http://mindprod.com/jgloss/boyer.html
    http://mindprod.com/jgloss/funduc.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 22, 2003
    #2
    1. Advertising

  3. text news

    Jim Guest

    Try this Perl script:


    # usage: perl thisscript.pl < oldFile >newFile
    #
    # feed this file to itself to see the substitions in
    # the lines at the end fo the file.

    # Loop over each line of input looking for
    # replacement strings and substituing the replacement
    # string for strings of x's.
    #
    while( <> )
    {
    #set the replacement string to whatever matches
    # 'photo' followed by and number ofdigits.
    # The match will assign whatever matched the the
    # contents of the parens to '$1'.
    #
    $replacement = $1 if ( m/(photo[0-9]+)[^0-9]/ );

    substitute one or more 'x' with the replacement.
    s/x+/$replacement/;

    # print out the line.
    print;
    }


    #rest of file is a string so perl not complain
    "

    text of file 1.

    "photo1
    blah blah blah blah
    xxxxx"

    text of file 2.

    "photo2
    blah blah blah blah
    xxxxx"

    text of file 3.

    "photo3
    blah blah blah blah
    xxxxx"

    text of file 99.

    "photo99
    blah blah blah blah
    xxxxx"

    ";
    "text news" <> wrote in message
    news:gNv1b.2049$...
    > I have already posted a little earlier today "Extract text to html href"

    and
    > please forgive this second post but I have given it some thought and this
    > may be a little more focused.
    > It is the same problem but maybe a much simpler approach.
    >
    > I need to do an advanced search and replace on a large number of files.
    > The search string is easy and identical in every file but the replace

    string
    > will be different on every file, although it can be easily identified and
    > exists in each file.
    >
    > For example:
    >
    > text of file 1.
    >
    > "photo1
    > blah blah blah blah
    > xxxxx"
    >
    > text of file 2.
    >
    > "photo2
    > blah blah blah blah
    > xxxxx"
    >
    > text of file 3.
    >
    > "photo3
    > blah blah blah blah
    > xxxxx"
    >
    > I want to replace xxxxx in each case with photo1, photo2 etc.
    >
    > I could do this with wild cards but need a kind of double search.
    > i.e. search for photo# and make this the replace string.
    > now search for xxxxx and replace it with the result of the first search.
    >
    > That must make more sense than my earlier posting.
    >
    > Thanks
    > Les
    >
    >
     
    Jim, Aug 23, 2003
    #3
  4. text news

    text news Guest

    Many thanks, problem solved, much appreciated.

    Les

    "Daniel Sjöblom" <_NOSPAM> wrote in message
    news:bi8qc8$c82$...
    > text news wrote:
    > > I have already posted a little earlier today "Extract text to html href"

    and
    > > please forgive this second post but I have given it some thought and

    this
    > > may be a little more focused.
    > > It is the same problem but maybe a much simpler approach.
    > >
    > > I need to do an advanced search and replace on a large number of files.
    > > The search string is easy and identical in every file but the replace

    string
    > > will be different on every file, although it can be easily identified

    and
    > > exists in each file.
    > >
    > > For example:
    > >
    > > text of file 1.
    > >
    > > "photo1
    > > blah blah blah blah
    > > xxxxx"
    > >
    > > text of file 2.
    > >
    > > "photo2
    > > blah blah blah blah
    > > xxxxx"
    > >
    > > text of file 3.
    > >
    > > "photo3
    > > blah blah blah blah
    > > xxxxx"
    > >
    > > I want to replace xxxxx in each case with photo1, photo2 etc.
    > >
    > > I could do this with wild cards but need a kind of double search.
    > > i.e. search for photo# and make this the replace string.
    > > now search for xxxxx and replace it with the result of the first search.
    > >
    > > That must make more sense than my earlier posting.
    > >
    > > Thanks
    > > Les
    > >
    > >

    > This will do. You'll probably want to add some more error checks, though.
    >
    > import java.util.regex.*;
    >
    > boolean photoReplace(StringBuffer buff) {
    > boolean success = false;
    > Matcher m = Pattern.compile("photo\\d+").matcher(buff);
    >
    > if (m.find()) {
    > String replacement = m.group();
    > int index = buff.lastIndexOf("xxxxx");
    > if (index != -1) {
    > buff.replace(index, buff.length(), replacement);
    > success = true;
    > }
    > }
    > return success;
    > }
    > --
    > Daniel Sjöblom
    >
    >
    >
     
    text news, Aug 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,317
    Thomas Weidenfeller
    Jan 21, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,507
    Victor Bazarov
    Feb 17, 2005
  3. AviraM
    Replies:
    2
    Views:
    6,386
    Manish Pandit
    Sep 28, 2006
  4. Abby Lee
    Replies:
    5
    Views:
    421
    Abby Lee
    Aug 2, 2004
  5. Replies:
    1
    Views:
    519
    Rainer Weikusat
    Jun 21, 2012
Loading...

Share This Page