跳转到内容

Rebol 编程/语言特性/解析/解析示例

来自维基教科书,开放的书籍,构建开放的世界

简单分割

[编辑 | 编辑源代码]

这是一个使用解析表达式匹配进行简单分割的示例。与 NONE 规则不同,此示例不会特别对待引号。可以轻松修改以满足您的需求。

simply-split: func [
    input [string!]
    /local delimiter whitespace regular result split
] [
    delimiter: charset ",;"
    whitespace: charset [#"^A" - #" " "^(7F)^(A0)"]
    regular: complement union delimiter whitespace
    ; result is a block containing the splits we collect
    result: copy []
    ; turn off the default whitespace handling,
    ; since we handle whitespace explicitly
    parse/all input [
        ; skip the leading whitespace
        any whitespace
        any [
            ; no split encountered
            delimiter
            (append result copy "")
            any whitespace
            | copy split some regular
            (append result split)
            any whitespace
            [delimiter any whitespace |]
        ]
    ]
    result
]

SIMPLY-SPLIT 函数的行为类似于 PARSE 函数,它获取 NONE 规则(上面的示例),但 CSV 的情况除外。

simply-split {"red","blue","green"}
; == [{"red"} {"blue"} {"green"}]

单词的 Porter 度量

[编辑 | 编辑源代码]
; vowel variants
vowel-after-consonant: charset "aeiouyAEIOUY"
vowel-otherwise: charset "aeiouAEIOU"

; consonant variants
consonant-after-consonant: exclude charset [
    #"a" - #"z" #"A" - #"Z"
] vowel-after-consonant
consonant-otherwise: union consonant-after-consonant charset "yY"

; adjusting the Vowel and Consonant rules to the Otherwise state
otherwise: first [
    (
        ; vowel detection does not change state
        vowel: vowel-otherwise
        ; consonant detection changes state to After-consonant
        consonant: [consonant-otherwise after-consonant]
    )
]

; adjusting the Vowel and Consonant rules to the After-consonant state
after-consonant: first [
    (
        ; vowel detection provokes transition to the Otherwise state
        vowel: [vowel-after-consonant otherwise]
        ; consonant detection does not change state
        consonant: consonant-after-consonant
    )
]

measure: [
    ; initialization
    (
        ; zeroing the counter
        m: 0
    )

    ; setting the state to Otherwise
    otherwise
    ; initialization end

    any consonant
    any [some vowel some consonant (m: m + 1)]
    any vowel
]
华夏公益教科书