Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

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


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.

On May 31, 8:57 am, alain@gmail.com 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
On May 31, 4:43 pm, Xicheng Jia <xich@gmail.com> wrote:

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.

alain@gmail.com 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

alain@gmail.com writes:
> 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.

Then you should ask your question in a group that discusses WSH or .NET.

Either that, or install ActiveState's WSH integration for Perl. :-)

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Sherm Pendley <spamt@dot-app.org> writes:
> alain@gmail.com writes:

>> 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.

> Then you should ask your question in a group that discusses WSH or .NET.

My apologies - I hadn't noticed that you *did* asked in those groups too.

Still, the principle is the same - if you don't want a Perl answer, you
shouldn't ask in a Perl group. Regexes are *not* generic - a regex that
works in WSH or .NET may not work in Perl, and vice-versa.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

["Followup-To:" header set to comp.lang.perl.misc.]

alain@gmail.com <alain@gmail.com> wrote:
> On May 31, 4:43 pm, Xicheng Jia <xich@gmail.com> wrote:
>> On May 31, 8:57 am, alain@gmail.com 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
    t@augustmail.com                   Perl programming
    Fort Worth, Texas

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc