跳轉到內容

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
]
華夏公益教科書