How to find and replace something that is nested inside something else?

Discussion in 'Perl Misc' started by alainfri@gmail.com, May 31, 2007.

  1. Guest

    I am not sure if this group is the right place for this question but
    what I need is as follows. There is a piece of html. Throughout the
    html there are a lot of <br> tags. The task is to replace all these
    <br> tags with \n\r. The replacement must be performed only within
    <pre> blocks. I can do this using VBScript in the following way:



    Option Explicit

    dim path2folder : path2folder = "D:\"
    dim path2file : path2file = path2folder & "test.htm"
    dim fileResults : fileResults = path2folder & "test-results.txt"
    dim text


    dim regex
    Set regex = New RegExp
    regex.Global = True

    Dim regex1
    Set regex1 = New RegExp
    regex1.Pattern = "<[Bb][Rr][\/r]{0,1}>"
    regex1.Global = True


    Dim matches, match, tmp, tmp1


    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim dfile : Set dfile = FSO.OpenTextFile( path2file, 1)

    If dfile.AtEndOfLine <> True Then
    text =dfile.ReadAll
    dfile.Close

    '///////////////////////////////////////////
    ' START OF RELEVANT CODE
    regex.Pattern = "<pre>.*?<\/pre>"
    Set matches = regex.Execute(text)
    For Each match In matches

    tmp = Match
    regex1.Pattern = "<[Bb][Rr][\/r]{0,1}>"
    tmp1 = regex1.Replace(tmp,vbCrlf)
    text = Replace(text,tmp,tmp1)
    Next
    ' END OF RELEVANT CODE
    '///////////////////////////////////////////
    Dim outfile : Set outfile = FSO.CreateTextFile(fileResults, True)

    outfile.WriteLine text
    outfile.Close
    MsgBox "OK"
    End If


    The question is how to achieve the same results using one call of
    regex.Replace, like

    'THIS DOES NOT WORK
    regex.Pattern = "(<pre>[.\n\r]*)(<[Bb][Rr][\/r]{0,1}>)([.\n\r]*</
    pre>)"
    text = regex.Replace(text, "$1" & vbCrlf & vbCrlf & "$3")

    Than you.
    , May 31, 2007
    #1
    1. Advertising

  2. Xicheng Jia Guest

    On May 31, 8:57 am, wrote:
    > I am not sure if this group is the right place for this question but
    > what I need is as follows. There is a piece of html. Throughout the
    > html there are a lot of <br> tags. The task is to replace all these
    > <br> tags with \n\r. The replacement must be performed only within
    > <pre> blocks. I can do this using VBScript in the following way:
    >


    If you are asking for Perl solutions, then here is one way to go:

    $string =~ s{<pre>(.*?)</pre>}{ mkCrtl($1) }egs;

    sub mkCrtl {
    my $str = shift;
    $str =~ s{<br>}{\n\r}g;
    return "<pre>$str</pre>";
    }

    Regards,
    Xicheng
    Xicheng Jia, May 31, 2007
    #2
    1. Advertising

  3. Guest

    On May 31, 4:43 pm, Xicheng Jia <> wrote:
    > On May 31, 8:57 am, wrote:
    >
    > > I am not sure if this group is the right place for this question but
    > > what I need is as follows. There is a piece of html. Throughout the
    > > html there are a lot of <br> tags. The task is to replace all these
    > > <br> tags with \n\r. The replacement must be performed only within
    > > <pre> blocks. I can do this using VBScript in the following way:

    >
    > If you are asking for Perl solutions, then here is one way to go:
    >
    > $string =~ s{<pre>(.*?)</pre>}{ mkCrtl($1) }egs;
    >
    > sub mkCrtl {
    > my $str = shift;
    > $str =~ s{<br>}{\n\r}g;
    > return "<pre>$str</pre>";
    >
    > }
    >
    > Regards,
    > Xicheng


    Thank you for the fast reply, Xicheng. Actually I need a solution that
    would allow me to do this using the regular expressions provided by
    Windows Script Host or by .NET Framework.
    , May 31, 2007
    #3
  4. Re: How to find and replace something that is nested inside somethingelse?

    wrote:
    > I am not sure if this group is the right place for this question but
    > what I need is as follows. There is a piece of html. Throughout the
    > html there are a lot of <br> tags. The task is to replace all these
    > <br> tags with \n\r. The replacement must be performed only within
    > <pre> blocks.


    Why? I thought that <br> tags (and other html) was properly rendered
    also within <pre> tags.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 31, 2007
    #4
  5. ["Followup-To:" header set to comp.lang.perl.misc.]

    <> wrote:
    > On May 31, 4:43 pm, Xicheng Jia <> wrote:
    >> On May 31, 8:57 am, wrote:
    >>
    >> > I am not sure if this group is the right place for this question but



    This is the right place to ask Perl questions.


    >> If you are asking for Perl solutions, then here is one way to go:
    >>
    >> $string =~ s{<pre>(.*?)</pre>}{ mkCrtl($1) }egs;
    >>
    >> sub mkCrtl {
    >> my $str = shift;
    >> $str =~ s{<br>}{\n\r}g;
    >> return "<pre>$str</pre>";
    >>
    >> }



    > Actually I need a solution that
    > would allow me to do this using the regular expressions provided by
    > Windows Script Host or by .NET Framework.



    Then you don't have a Perl question!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 1, 2007
    #5
    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. Guest
    Replies:
    4
    Views:
    475
    Guest
    Oct 13, 2004
  2. Replies:
    25
    Views:
    659
    MonkeeSage
    Oct 1, 2006
  3. Melroy
    Replies:
    6
    Views:
    1,327
    David Thompson
    Dec 22, 2008
  4. kj
    Replies:
    15
    Views:
    548
    Lawrence D'Oliveiro
    May 23, 2009
  5. A
    Replies:
    8
    Views:
    795
    James Kanze
    Aug 28, 2010
Loading...

Share This Page