如何实现“替代”在java中
“supplant() 对字符串进行变量替换。它会扫描字符串,查找包含在 { } 大括号中的表达式。如果找到表达式,则将其用作对象上的键,并且如果该键具有字符串值或数字值,它被替换为括号表达式并重复。”
更具体地说,我正在尝试做一些类似的事情:
public static String supplant(CharSequence message, Map<String,Object> params)
我正在尝试使用像 \{([\w]*)\} 这样的正则表达式来捕获变量,然后查找地图并替换,但我无法编写正则表达式匹配...
你如何在java中实现supplant()?
"supplant() does variable substitution on the string. It scans through the string looking for expressions enclosed in { } braces. If an expression is found, use it as a key on the object, and if the key has a string value or number value, it is substituted for the bracket expression and it repeats."
More specifically, i am trying to do somethink like:
public static String supplant(CharSequence message, Map<String,Object> params)
I am trying to use a regex like \{([\w]*)\} to capture the variables and then lookup the map and replace, but i couldn't write a regex that matches...
How would you implement supplant() in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它需要使用这种确切的格式吗? Java 已经具有将变量替换为字符串的内置方法。
请参阅:
Does it need to work with this exact format? Java already has built-in methods for substituting variables into strings.
See:
您所描述的内容通常称为“模板化”。有许多模板应用程序和库,但如果您只是想要一个快速而肮脏的解决方案,则可以使用正则表达式轻松实现。
在许多正则表达式风格中,您将使用内置的callback或callout机制,例如PHP的
preg_replace_callback
函数或Perl的/e< /code> 和
/ee
修饰符。 Java没有类似的东西,但它提供了API让你自己实现它。下面是一个示例:显然,此代码遗漏了一些基本组件(主要是异常处理),但它演示了此技术的要点:
group(1)< 来访问它/code> 在匹配器上。
appendReplacement(sb, "")
附加上一个匹配项(如果有)和当前匹配项之间出现的任何文本。append()
方法追加它。 (您可以通过将替换字符串作为第二个参数传递给appendReplacement()
来合并步骤2和3,但是随后您必须注意美元符号和反斜杠字符串,接受特殊处理。这种方式要简单得多。)appendTail(sb)
附加最后一次匹配后剩下的内容。有几个人已经发布了帮助程序类,可以让您完成此类任务,而无需编写所有样板代码。我最喜欢的是 Elliott Hughes 的 重写器。
What you describe is more commonly called templating. There are many templating applications and libraries out there, but if you just want a quick-and-dirty solution, it can easily be implemented with regexes.
In many regex flavors you would use a built-in callback or callout mechanism, like PHP's
preg_replace_callback
function, or Perl's/e
and/ee
modifiers. Java doesn't have anything like that, but it provides the API to let you implement it yourself. Here's an example:Obviously this code leaves out some essential components (principally exception handling), but it demonstrates the main points of this technique:
group(1)
on the Matcher.appendReplacement(sb, "")
to append whatever text appeared between the previous match (if there was one) and the current one.append()
method. (You could combine steps 2 and 3 by passing the replacement string as the second argument toappendReplacement()
, but then you would have to watch out for dollar signs and backslashes in the string, which receive special treatment. This way is much simpler.)appendTail(sb)
to append whatever's left after the last match.Several people have published helper classes that let you accomplish this sort of task without having to write all that boilerplate code. My favorite is Elliott Hughes's Rewriter.