如何使用string.replaceAll来更改某个单词之后的所有内容

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

How to use string.replaceAll to change everything after a certain word

问题

我有以下字符串: http://localhost:somePort/abc/soap/1.0

我想让字符串看起来只是这样: http://localhost:somePort/abc

我想使用string.replaceAll,但似乎无法得到正确的正则表达式。我的代码看起来是这样的: someString.replaceAll(".*\\babc\\b.*", "abc");

我想知道我漏掉了什么?我不想拆分字符串或使用.replaceFirst,尽管有很多解决方案建议这样做。

英文:

I have the following string: http://localhost:somePort/abc/soap/1.0

I want the string to just look like this: http://localhost:somePort/abc.

I want to use string.replaceAll but can't seem to get the regex right. My code looks like this: someString.replaceAll(".*\\babc\\b.*", "abc");
I'm wondering what I'm missing? I don't want to split the string or use .replaceFirst, as many solutions suggest.

答案1

得分: 1

以下是翻译好的部分:

"使用 substring 似乎更有道理,但如果你必须使用 replaceAll,下面是一种方法。

你想要将 /abc 及其后的所有内容替换为 /abc

string = string.replaceAll("/abc.*", "/abc");

如果你想更加精确,可以在 abc 后面包含一个单词边界,如下:

string = string.replaceAll("/abc\\b.*", "/abc");
```"

<details>
<summary>英文:</summary>

It would seem to make more sense to use `substring`, but if you must use `replaceAll`, here&#39;s a way to do it.

You want to replace `/abc` and everything after it with just `/abc`.

    string = string.replaceAll(&quot;/abc.*&quot;, &quot;/abc&quot;)

If you want to be more discriminating you can include a word boundary after `abc`, giving you

    string = string.replaceAll(&quot;/abc\\b.*&quot;, &quot;/abc&quot;)

</details>



# 答案2
**得分**: 1

关于给定正则表达式的解释,为什么它不起作用:

`\b` `\b` - 这里不需要单词边界,而且由于在开头添加了 `.*`,它会匹配整个字符串,当你尝试用 "abc" 替换它时,它将用 "abc" 替换整个匹配项。因此,你得到了错误的答案。相反,只尝试匹配所需的内容,然后将匹配的内容替换为 "abc" 字符串。

```java
someString.replaceAll("/abc.*", "/abc");

/abc.* - 专门查找以 /abc 开头,后跟0个或多个字符的字符串。
/abc - 用上面的匹配项替换。

英文:

Just for explanation on the given regex, why it wont work:

\b \b - word boundaries are not required here and also as .* is added in the beginning it matches the whole string and when you try to replace it with "abc" it will replace the entire match with "abc". Hence you get the wrong answer. Instead, only try to match what is required and then whatever is matched that will be replaced with "abc" string.

someString.replaceAll(&quot;/abc.*&quot;, &quot;/abc&quot;);

/abc.* - Looks specifically for /abc followed by 0 or more characters
/abc - Replaces the above match with /abc

答案3

得分: 0

你应该使用replaceFirst,因为在第一次匹配后,你要删除所有的内容

text = text.replaceFirst("/abc.*", "/abc");

或者

你可以使用indexOf来获取特定单词的索引,然后获取子串

String findWord = "abc";
text = text.substring(0, text.indexOf(findWord) + findWord.length());
英文:

You should use replaceFirst since after first match you are removing all after

text=  text.replaceFirst(&quot;/abc.*&quot;, &quot;/abc&quot;);

Or

You can use indexOf to get the index of certain word and then get substring.

String findWord = &quot;abc&quot;;
text = text.substring(0, text.indexOf(findWord) + findWord.length());

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

发表评论

匿名网友

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

确定