使用ReadP
考虑以下代码段:
resource "aws_security_group_rule" "sdfsdfsdf" {
count = "${length(data.google_compute_subnetwork.mysubnetwork.secondary_ip_range)}"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["${data.google_compute_subnetwork.mysubnetwork.secondary_ip_range[count.index]}.ip_cidr_range}"]
}
它输出
import Text.ParserCombinators.ReadP
readP_to_S (between (char '[') (char ']') (munch (/= ','))) "[234]"
根据文档
之间:: ReadP打开-> ReadP关闭-> ReadP a-> ReadP a Source#
在开闭之间,p解析为开,然后是p,最后是闭。仅返回p的值。
因此,我希望返回值为
[]
,因为第一个解析器与左括号匹配,第二个与右括号匹配,最后一个匹配除逗号以外的所有内容。为什么不这样做?
答案 0 :(得分:1)
between open close p = do _ <- open
x <- p
_ <- close
return x
很明显,p
解析器一定不能消耗close
解析器所需的字符(对我来说,这不是很直观)。
例如
readP_to_S (between (char '[') (char ']') (munch (/= ']'))) "[234]"
给予
[("234","")]