2014-11-20 4 views
4

HTML 페이지가있어서이 파일에서 몇 줄을 바꿔야합니다. 그러나 replace을 사용하면 단일 행보다 더 큰 것을 찾을 수 없습니다. REBOL을 사용하여 파일의 행에 걸쳐있는 문자열 찾기/바꾸기

내가 (페이지에서의 여러 인스턴스가) 교체하고자하는 것입니다 :

.... 
<div class="logo"> 
    <img src="resources/logo.svg" /> 
    <span>e</span> 
    <span class="text">Market</span> 
</div> 
... 

여기에 내가 노력하고 코드입니다,하지만 그것은 작동하지 않습니다

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 

logo-div에 줄 바꿈을 포함하는 ^/을 포함하면 도움이되지 않습니다.

어떻게이 전체 문자열을 찾을 수 있습니까?

은 (내가 Rebol2를 사용하고 있지만이 기능은 동일하거나 Rebol3 매우 유사합니다 가정합니다.)

답변

4

나는 replace에 대해 확실하지 않다 그러나 이것은 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 

색인 파일이 없어도 작동합니다. 신중하게 문자열 파싱을 연구 할 시간을 가져라. 그것은 매우 강력합니다.

+0

위대한 작품! 필자가 바꾸어야 할 유일한 것은'parse/all'을 사용하여 출력물에 여전히 공백을 넣었습니다. 귀하의 회신에 감사드립니다. –

+0

@JamesIrwin PARSE가 모든 태그를 "문자열 화"할 것이라고 언급 할 것입니다! 그것은 방언 행위로서 발견되는 요소들입니다. 따라서 ** title : [

(append out new-div)] **와 같은 규칙이 될 수 있습니다. Rebol의 소스 표기법은 모든 태그와 호환되지 않습니다. 따라서 ** {Market} **을 작성해야합니다. 예를 들어 중간에 문자열을 이어 붙일 수 있습니다. 하지만 여전히 유용합니다. 또한 [Rebol and Red SO Chat] (http://chat.stackoverflow.com/rooms/291/rebol-and-red) – HostileFork

2

내가 직면 한 문제는 요소 사이의 공백 개수가 가변적 인 것으로 의심됩니다. 당신이 공백을 트리밍 할 수있는 경우, 작업을해야 교체 :

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

을 당신이 HTML 들여 쓰기를 유지하려면, 아마 최선의 선택이 될 것입니다 kealist에 의해 제안 다음 구문 분석합니다.

+0

을 알지 못하는 경우 참고하십시오. :) –