I have an HTML page, and I need to replace a couple of lines in it. However, using replace
can't seem to find anything bigger than a single line.
我有一个HTML页面,我需要替换其中的几行。然而,使用replace似乎找不到比单行更大的东西。
This is what I would like to replace (there are multiple instances of it in the page):
这是我想替换的(页面中有多个实例):
....
<div class="logo">
<img src="resources/logo.svg" />
<span>e</span>
<span class="text">Market</span>
</div>
...
Here's the code I am trying, but it doesn't work:
这是我正在尝试的代码,但它不起作用:
index-html: read %index.html
logo-div: {<div class="logo">
<img src="resources/logo.svg" />
<span>e</span>
<span class="text">Market</span>
</div>}
new-div: {...}
out: replace/all index-html logo-div new-div
write %index.html out
Including ^/
in my logo-div
string to denote the newlines doesn't help.
包括^ /在我logo-div字符串表示换行不帮助。
How can I find this entire string?
我怎么能找到这整条线?
(I am using Rebol2, but I assume the functionality will be the same or very similar in Rebol3.)
(我正在使用Rebol2,但我假设Rebol3的功能是相同的或非常相似的。)
2 个解决方案
#1
4
I'm not sure about replace
but this is a good opportunity for parse
to work well:
我不太确定是否要替换,但这是一个很好的机会,可以让parse很好地工作:
index-html: read %index.html
out: copy ""
new-div: "..."
;;parse rules
title: [
{<div class="logo">} thru {</div>} (append out new-div) ;;appends replacement when it finds that class
]
rule: [
some [
title |
copy char skip (append out char) ;;copies every other char char
]
]
parse index-html rule
write %index.html out
Don't have the index file, but this should work. Take some time to carefully study string parsing. It is very powerful.
不要有索引文件,但是这个应该可以工作。花些时间仔细研究字符串解析。它是非常强大的。
#2
2
I suspect the issue you are facing is that of the variable number of spaces between elements. If you can trim out the whitespace, replace should work:
我怀疑您所面临的问题是元素之间的空间的可变数量。如果你能减少空白,替换应该工作:
>> data: {
{ line 1
{ line 2
{ line 3
{ line 4
{ <div>
{ line d1
{ line d2
{ </div>
{ line 5
{ line 6
{ }
== {
line 1
line 2
line 3
line 4
<div>
line d1
line d2
</div>
line 5
line 6
}
old-div: {
{ <div>
{ line d1
{ line d2
{ </div>
{ }
== {
<div>
line d1
line d2
</div>
}
>> new-div: {
{ <div>new line d1^/new line d2</div>
{ }
== "^/<div>new line d1^/new line d2</div>^/"
>> replace/all trim/all data trim/all old-div new-div
== {line1line2line3line4
<div>new line d1
new line d2</div>
line5line6}
>> data
== {line1line2line3line4
<div>new line d1
new line d2</div>
line5line6}
If you want to retain the HTML indentation, then parse as suggested by kealist would probably be the best option.
如果您希望保留HTML缩进,那么按键符建议进行解析可能是最好的选择。
#1
4
I'm not sure about replace
but this is a good opportunity for parse
to work well:
我不太确定是否要替换,但这是一个很好的机会,可以让parse很好地工作:
index-html: read %index.html
out: copy ""
new-div: "..."
;;parse rules
title: [
{<div class="logo">} thru {</div>} (append out new-div) ;;appends replacement when it finds that class
]
rule: [
some [
title |
copy char skip (append out char) ;;copies every other char char
]
]
parse index-html rule
write %index.html out
Don't have the index file, but this should work. Take some time to carefully study string parsing. It is very powerful.
不要有索引文件,但是这个应该可以工作。花些时间仔细研究字符串解析。它是非常强大的。
#2
2
I suspect the issue you are facing is that of the variable number of spaces between elements. If you can trim out the whitespace, replace should work:
我怀疑您所面临的问题是元素之间的空间的可变数量。如果你能减少空白,替换应该工作:
>> data: {
{ line 1
{ line 2
{ line 3
{ line 4
{ <div>
{ line d1
{ line d2
{ </div>
{ line 5
{ line 6
{ }
== {
line 1
line 2
line 3
line 4
<div>
line d1
line d2
</div>
line 5
line 6
}
old-div: {
{ <div>
{ line d1
{ line d2
{ </div>
{ }
== {
<div>
line d1
line d2
</div>
}
>> new-div: {
{ <div>new line d1^/new line d2</div>
{ }
== "^/<div>new line d1^/new line d2</div>^/"
>> replace/all trim/all data trim/all old-div new-div
== {line1line2line3line4
<div>new line d1
new line d2</div>
line5line6}
>> data
== {line1line2line3line4
<div>new line d1
new line d2</div>
line5line6}
If you want to retain the HTML indentation, then parse as suggested by kealist would probably be the best option.
如果您希望保留HTML缩进,那么按键符建议进行解析可能是最好的选择。