一次替换多个子字符串
假设我有一个文件,其中包含一些文本。其中有“substr1”、“substr2”、“substr3”等子字符串。我需要将所有这些子字符串替换为其他文本,例如“repl1”、“repl2”、“repl3”。在Python中,我会创建一个像这样的字典:
{
"substr1": "repl1",
"substr2": "repl2",
"substr3": "repl3"
}
并创建用“|”连接键的模式,然后用re.sub
函数替换。 Java 中有类似的简单方法吗?
Say I have a file, that contains some text. There are substrings like "substr1", "substr2", "substr3" etc. in it. I need to replace all of those substrings with some other text, like "repl1", "repl2", "repl3". In Python, I would create a dictionary like this:
{
"substr1": "repl1",
"substr2": "repl2",
"substr3": "repl3"
}
and create the pattern joining the keys with '|', then replace with re.sub
function.
Is there a similar simple way to do this in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是您的 Python 建议转换为 Java 的方式:
此方法执行同时(即“一次”)替换。即,如果您碰巧有
,那么这种方法将给出
"a b" -> “b c”
而不是建议您应该链接多个调用replace
或replaceAll
的答案,后者会给出“c c”
。(如果您将此方法推广为以编程方式创建正则表达式,请确保您
Pattern.quote
每个单独的搜索词和Matcher.quoteReplacement
每个替换词。)This is how your Python-suggestion translates to Java:
This approach does a simultanious (i.e. "at once") replacement. I.e., if you happened to have
then this approach would give
"a b" -> "b c"
as opposed to the answers suggesting you should chain several calls toreplace
orreplaceAll
which would give"c c"
.(If you generalize this approach to create the regexp programatically, make sure you
Pattern.quote
each individual search word andMatcher.quoteReplacement
each replacement word.)StringUtils.replaceEach
在 Apache Commons Lang 项目,但它适用于字符串。StringUtils.replaceEach
in the Apache Commons Lang project, but it works on Strings.首先,演示一下问题:
这是为了替换cats =>狗和狗=> budgies,但是顺序替换对前一个替换的结果进行操作,因此不幸的输出是:
这是我的同时替换方法的实现。使用
String.regionMatches
:测试:
输出:
此外,在进行同时替换时有时很有用,以确保寻找最长的匹配。 (PHP 的
strtr
函数就是这样做的,对于示例。)这是我的实现:为什么需要这个?示例如下:
输出:
如果我们使用
simultaneousReplace
而不是simultaneousReplaceLongest
,输出将是“HamScript”而不是“Hamster”:)请注意上述方法区分大小写。如果您需要不区分大小写的版本,则可以轻松修改上述内容,因为
String.regionMatches
可以采用ignoreCase
参数。First, a demonstration of the problem:
This is intended to replace cats => dogs and dogs => budgies, but the sequential replacement operates on the result of the previous replacement, so the unfortunate output is:
Here's my implementation of a simultaneous replacement method. It's easy to write using
String.regionMatches
:Testing:
Output:
Additionally, it is sometimes useful when doing simultaneous replacement, to make sure to look for the longest match. (PHP's
strtr
function does this, for example.) Here is my implementation for that:Why would you need this? Example follows:
Output:
If we had used
simultaneousReplace
instead ofsimultaneousReplaceLongest
, the output would have had "HamScript" instead of "Hamster" :)Note that the above methods are case-sensitive. If you need case-insensitive versions it is easy to modify the above because
String.regionMatches
can take anignoreCase
parameter.