gpt4 book ai didi

java - 将 Java 模式 replaceAll 转换为 C#

转载 作者:行者123 更新时间:2023-11-30 10:12:35 26 4
gpt4 key购买 nike

我有这段 Java 代码需要转换成 C#

private static final Pattern MISSING_PRODUCT_AT_START = Pattern.compile("^\\(( |;|null|compatible|windows|android|linux).*", Pattern.CASE_INSENSITIVE);

result = MISSING_SPACE.matcher(result).replaceAll("$1 $2");

第一部分可以用正则表达式转换为:

 private static readonly Regex MISSING_SPACE = new Regex("(/[0-9]+\\.[0-9]+)([A-Z][a-z][a-z][a-z]+ )", RegexOptions.IgnoreCase);

但是关于第二部分,我不知道Java replaceAll到底是什么意思,我不是Java专家

为了解释上下文,我正在将一个开源库从 Java 翻译到 .NET

如何将 .replaceAll("$1 $2"); 翻译成 C#?

最佳答案

通过阅读 replaceAll 的文档,我们可以看到它是这样做的:

Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.

Regex.Replace相同在 .NET 中做:

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

所以你可以把这行翻译成::

result = MISSING_SPACE.Replace(result, "$1 $2");

replaceAll 中的单词 all 有点暗示它做了一些与 Replace 不同的事情,但我认为这只是为了将它与 replaceFirstreplaceLast

关于java - 将 Java 模式 replaceAll 转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51832235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com