在Rebol PARSE中,如何检查输入的开始?

时间:2018-09-18 18:59:52

标签: parsing rebol

我需要一个规则,让它叫做AT-BEGIN,它与输入的开头相匹配。

也许它存在,或者如何实现?

我想工作的示例:

  1. 解析“ x” [AT-BEGIN“ x”] =>匹配
  2. 将“ {some-other-chars} x”解析为[x“ AT-BEGIN” x“] =>不匹配

动机:

我正在尝试解析一些类似于markdown的模式,其中有一个'*' 开始强调,但前提是空格后或开始时 文本: [空间|从头开始] 强调

使用@hostilefork解决方案,我可以这样写: [to "*" pos: [if (head? pos) | (pos: back pos) :pos space] skip ...]

2 个答案:

答案 0 :(得分:1)

There's no such rule. But if there were, you'd have to define if you specifically want to know if it's at the beginning of a series...or at the beginning of where you were asked to start the parse from. e.g. should this succeed or fail?

parse (next "cab") [to "a" begin skip "b"]

It's not at the beginning of the series but the parse position has not moved. Does that count as the beginning?

If you want a test just for the beginning of the series:

[to "a" pos: if (head? pos) ...]

You'd have to capture the position at the beginning or otherwise know it to see if the parse position had advanced at all:

[start: to "a" pos: if (pos = start) ...]

答案 1 :(得分:0)

也许不完全是您想要的,但是可以通过以下方法找到第一个匹配项。

您必须搜索“ b”后跟“ a”的补码形式。

>> text-to-searh: "jjjj ball  adran"
>> b*: complement charset "b"
>> parse/all text-to-search [ any [ b* #"a" | "a" hit:  to end | skip ] ]
>> probe hit
"ll  adran"

需要一些修改以匹配第一个字符为“ a”的情况。 hit将位于“ a”之后的字符处。