简体   繁体   English

Rebol /红色解析:如何在2个标记之间复制

[英]Rebol/Red parse: how to copy between 2 marks

I want to be able to parse between 2 marks in parse rule. 我希望能够在解析规则中解析2个标记之间。 For a contrieved example: 举一个人为的例子:

src: {a b c d e f}

rule: [
    to "b" mark1: thru "e" mark2: 
    to mark1 copy text to mark2
]

This doesn't work, text contains "[" instead of what I'd like to get: 这不起作用,文本包含“ [”,而不是我想要的内容:

b c d e

You're trying to implement a "DO desire" of copying using PARSE. 您正在尝试实现使用PARSE复制的“ DO愿望”。 PARSE's COPY is looking for patterns, not treating the series as positions. PARSE的COPY正在寻找模式,而不是将系列视为头寸。

You can escape into DO in mid-parse via a PAREN!, it will run if the parse rule reaches that point. 您可以通过PAREN!在解析过程中转入DO,如果解析规则到达该点,它将运行。

src: {a b c d e f}

rule: [
    to "b" mark1: thru "e" mark2: 
    (text: copy/part mark1 mark2)
    to end ;-- not strictly necessary, but makes PARSE return true
]

parse src rule

That will give you text as bcde 那会给你文本为bcde

Note that you can't have it both ways, either with COPY or with TO. 请注意,使用COPY或TO不能同时使用这两种方法。 TO <series!> meant "look for b", not "jump to the position of b". TO <series!>意思是“寻找b”,而不是“跳到b的位置”。 So when you say to mark1 you're invoking another match. 因此,当您说到to mark1您正在调用另一个匹配项。 If you want to set the parse position to the specific position recorded in mark1, use :mark1 in the parse rule. 如果要将解析位置设置为:mark1中记录的特定位置 ,请在解析规则中使用:mark1

two alternative solutions/rules working in Red Red中的两个替代解决方案/规则

rule: [
   to "b" copy text thru "e" to end
]

and

 rule: [ to "b" collect  [keep thru "e"] to end]
 text: first parse src rule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM