替换所有匹配给定模式的出现次数。

huangapple go评论72阅读模式
英文:

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 = &quot;/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&quot;`

In need to replace values of `run_key` and `run_key_default` with `%`, for example, for above string result output will be the:

`&quot;/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else&quot;`

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(&quot;\run_key=[*]\&quot;, &quot;%&quot;).replaceAll(&quot;\run_key_default=[*]\&quot;, &quot;%&quot;)`

I&#39;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:

    &quot;$1%&quot;

[RegEx Demo][1]


  [1]: https://regex101.com/r/cSyN90/1

**Java Code:**

    String output = value.replaceAll(&quot;(/run_key(?:_default)?=)[^/]*&quot;, &quot;$1%&quot;);

**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 `/`

- `&quot;$1%&quot;` 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 = &quot;(run_key_default|run_key)=\\w*&quot;; //regex
    		final String string = &quot;/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&quot;;
    		final String subst = &quot;$1=%&quot;; //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(&quot;Substitution result: &quot; + 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>



huangapple
  • 本文由 发表于 2020年8月13日 04:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63383964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定