我想${*}
从字符串中提取表达式。
val example = "Hello ${foo.bar} World"
但是美元符号和大括号是表达式语法的一部分,因此我尝试对其进行转义。
val expr = "\\$\\{[a-zA-Z0-9\\w]*\\}".r
但这不起作用,println
什么也不打印:
for (ex <- expr.findAllMatchIn(example)) println(ex)
有人知道出什么事了吗?是否存在更优雅的正则表达式?
那是因为您没有考虑.
in foo.bar
,而是这样做:
\\$\\{[a-zA-Z0-9.\\w]*\\} // allows for a . to also be in the string
为了获得最大的灵活性,您可以执行以下操作:
\\$\\{[^}]*.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句