REBOL Programming/Language Features/Parse/Parsing examples
< REBOL Programming < Language Features < ParseSimply split
This is an example of simple splitting using parse expression matching. As opposed to the NONE rule, this example does not treat the quotation marks specially. It can be modified easily to suit your needs.
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
]
The SIMPLY-SPLIT function behaves like the PARSE function obtaining the NONE rule (examples above) except for the CSV case:
simply-split {"red","blue","green"} ; == [{"red"} {"blue"} {"green"}]
Porter measure of a word
; 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
]
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.