我想解析一个字符串,找到(character, n)
集中找到的前N个重复字符。
例如,对于"ozzllluu"
和设置(" u" => 2),(" d" => 2),(" l& #34; => 3),和(" r" => 3)...我想找到" lll",因为它是3个字符并且发生在两个" u" s。
程序式解决方案:
Rebol []
seq-set: [#"u" 2 #"d" 2 #"l" 3 #"r" 3]
str: "ozzllluu"
lastchar: ""
cnt: 1
seq-char: ""
foreach char str [
either char = lastchar [
cnt: cnt + 1
if (select seq-set char) = cnt [
seq-char: char
break
]
][
cnt: 1
]
lastchar: char
]
either seq-char = "" [
print "no seq-char"
][
print join "seq-char " seq-char
]
如何使用parse
规则执行同样的操作?
简而言之:
parse
字符串,用于在(字符,n)集答案 0 :(得分:8)
这是使用Red的Parse(也在R3中工作)的解决方案:
seq-set: [2 #"u" | 2 #"d" | 3 #"l" | 3 #"r"]
rule: [any [set char seq-set break | skip]]
red>> parse "ozzllluu" rule
red>> char
== #"l"
答案 1 :(得分:3)
只是一个简单的规则作为起点
keys: ["uu" | "dd" | "lll" | "rrr"]
rule: [(k: none) any [[copy k keys to end ] | skip] ]
>> parse "olllddsslll rr rrr" rule k
== "lll"
答案 2 :(得分:1)
parse
以下规则会查找所有重复内容并跳过其他内容。
;Rebol 2 version
char: charset [#"a" - #"z"]
parse/all "wqooossssccfgg" [some [
copy x char [copy y some x (print [s: join x y length? s])]
| skip
]
]
;output
ooo 3
ssss 4
cc 2
gg 2
;Red version
parse "wqooossssccfgg" [some [
copy x char [copy y some x (print [s: append x y length? s])]
| skip ]]