英文:
Replace all occurrences matching given patterns
问题
Sure, here's the translated code part:
有以下字符串:
`String value = "/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=32ee/group_key=222/end_date=2020-04-20/run_key_default=32sas1/somethingElse=else"`
需要将 `run_key` 和 `run_key_default` 的值替换为 `%`,例如,对于上述字符串,结果输出将为:
`"/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else"`
我想避免误修改其他值,所以我认为最好的解决方案是将 `replaceAll` 方法与正则表达式结合使用:
```java
String output = value.replaceAll("\\b(run_key=\\S+)\\", "%").replaceAll("\\b(run_key_default=\\S+)\\", "%");
我不确定如何构建正则表达式?如果您知道比我提供的更好的解决方案,请随时发布。
<details>
<summary>英文:</summary>
Having following string:
`String value = "/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=32ee/group_key=222/end_date=2020-04-20/run_key_default=32sas1/somethingElse=else"`
In need to replace values of `run_key` and `run_key_default` with `%`, for example, for above string result output will be the:
`"/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else"`
I would like to avoid mistakenly modifying other values, so in my opinion the best solution for it is combining `replaceAll` method with `regex`
`String output = value.replaceAll("\run_key=[*]\", "%").replaceAll("\run_key_default=[*]\", "%")`
I'm not sure how should I construct regex for it?
Feel free to post if you know better solution for it, than this one which I provided.
</details>
# 答案1
**得分**: 4
String output = value.replaceAll("(/run_key(?:_default)?=)[^/]*", "$1%");
正则表达式详情:
- `(`: 开始捕获组 #1
- `/run_key`: 匹配文字 `/run_key`
- `(?:_default)?`: *可选地*匹配 `_default`
- `=`: 匹配文字 `=`
- `)`: 结束捕获组 #1
- `[^/]*`: 匹配0个或多个非 `/` 字符
- `"$1%"` 是**替换**,它将我们的第一个捕获组放回,后面跟着文字 `%`。
<details>
<summary>英文:</summary>
You may use this regex for search:
(/run_key(?:_default)?=)[^/]*
and for replacement use:
"$1%"
[RegEx Demo][1]
[1]: https://regex101.com/r/cSyN90/1
**Java Code:**
String output = value.replaceAll("(/run_key(?:_default)?=)[^/]*", "$1%");
**RegEx Details:**
- `(`: Start capture group #1
- `/run_key`: Match literal text `/run_key`
- `(?:_default)?`: Match `_default` *optionally*
- `=`: Match a literal `=`
- `)`: End capture group #1
- `[^/]*`: Match 0 or more of any characters that is not `/`
- `"$1%"` is **replacement** that puts our 1st capture group back followed by a literal `%`
</details>
# 答案2
**得分**: 1
Substitution result:
/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else
<details>
<summary>英文:</summary>
public static void main(String[] args) {
final String regex = "(run_key_default|run_key)=\\w*"; //regex
final String string = "/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=32ee/group_key=222/end_date=2020-04-20/run_key_default=32sas1/somethingElse=else";
final String subst = "$1=%"; //group1 as it is while remaining part with %
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
output
Substitution result:
/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论