473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 <brtags. The task is to replace all these
<brtags with \n\r. The replacement must be performed only within
<preblocks. 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 '07 #1
6 1715
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 <brtags. The task is to replace all these
<brtags with \n\r. The replacement must be performed only within
<preblocks. 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

May 31 '07 #2
On May 31, 4:43 pm, Xicheng Jia <xich...@gmail.comwrote:
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 <brtags. The task is to replace all these
<brtags with \n\r. The replacement must be performed only within
<preblocks. 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 '07 #3
al******@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 <brtags. The task is to replace all these
<brtags with \n\r. The replacement must be performed only within
<preblocks.
Why? I thought that <brtags (and other html) was properly rendered
also within <pretags.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
May 31 '07 #4
al******@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
May 31 '07 #5
Sherm Pendley <sp******@dot-app.orgwrites:
al******@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
May 31 '07 #6
["Followup-To:" header set to comp.lang.perl.misc.]

al******@gmail.com <al******@gmail.comwrote:
On May 31, 4:43 pm, Xicheng Jia <xich...@gmail.comwrote:
>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
ta***@augustmail.com Perl programming
Fort Worth, Texas
May 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
2
by: Jim Heavey | last post by:
I am trying to figure out what the difference is between these 2 commands and which I should be using...what the difference is... I placed this tag on my form... <div id="fred"...
6
by: TomislaW | last post by:
How to find all user controls (ascx) loaded on a Page?
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
0
by: Fabio R. | last post by:
I'm going crazy... Can someone help me to do a regex to search for something like nested if? I have these tags: <if>...<else>...<endif> that can be nested: <if> <if>...<else>...<endif>
10
by: wazzup | last post by:
C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far. Create a program to print nested squares Input:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.