"I want to choose F12 or F 12 from F1(a), F2a, F5-A, F34-5 and so on, but F alone should not be chosen, that is, choose those which start with F followed by a digit (before the digit there could be zero or more than one space) and then by any character from ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ['(',')',"-"]。
结果应该是[F12, F12, F1(a), F2a, F5-A, F34-5],其中F和数字之间的空格应该被删除。
使用parsec,我已经成功地获取了一个子字符串,如F12,F2a。 代码如下:
hao :: Parser Char
hao = oneOf "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ()-"
tuhao :: Parser String
tuhao = do { c <- char 'F'
; many space
; c1 <- digit
; cs <- many hao
; return (c:c1:cs)
}
parse tuhao "" str -- can parse the str and get one sub-string.
然而,我卡在了如何解析上述示例字符串并获取特定模式的所有子字符串上。我的想法是,如果找到F,则开始解析,否则跳过解析;或者如果解析失败,则跳过解析。但我不知道如何实现这个计划。我有另一个想法,使用State
记录未解析的剩余字符串,并使用递归,但仍无法执行。
所以我非常感谢任何提示!^_^
["F12", "F12", "F1(a)", "F2a", "F5-A", "F34-5"]
),但没有给出输入。 - Adam Smith[m| m<- subsequences "F1(a)F2aF5-AF34-5", elem m ["F12","F 12"]]
我得到了一个 ["F12"],这很可能是正确的。subsequence
按字符序列化,因此无意义的字符可以被删除。我只去掉了空格和逗号。 - fp_mora