标签: replaceall

为什么replaceAll会从字符串中删除数字?

String foo = "a3#4#b";
String afterPunctutationRemoval = foo.replaceAll("[,.;:?!'-_\"/()\\{}]", "");
System.out.println(afterPunctutationRemoval);
Run Code Online (Sandbox Code Playgroud)

它给了我一个"## b"的结果,有人可以解释我为什么吗?

它不应该按原样返回字符串吗?

java regex string replaceall

3
推荐指数
2
解决办法
293
查看次数

Java String.replaceAll不替换并发重复序列

我正在尝试使用String.replaceAll从字符串中删除一些空格.但是,当正则表达式模式的多个并发实例出现在字符串中时,只替换每个第二个实例.

很简单的例子:

String theString = "foo x x x x x bar";        
String trimmed = theString.replaceAll("x\\s*x", "xx");        
System.out.println(theString);
System.out.println(trimmed);
Run Code Online (Sandbox Code Playgroud)

我想看到的:

foo x x x x x bar
foo xxxxx bar
Run Code Online (Sandbox Code Playgroud)

我所看到的:

foo x x x x x bar
foo xx xx x bar
Run Code Online (Sandbox Code Playgroud)

似乎replaceAll不会将替换文本视为自己被替换的候选者,而是快速地向前跳过.

这有一个简单的解决方案吗?

java regex string replaceall

3
推荐指数
1
解决办法
242
查看次数

Java:替换\"with"和\/with /

我正在创建json字符串以发送到服务器.我使用GSon生成json字符串.因为我的服务器不理解的字符串,如:\"key\":value"key":value还是"a\/b"不过a/b.所以在生成json字符串之后,我使用replaceAll方法来解决这个问题:

String res = original.replaceAll("\\/", "/").replaceAll("\\"", "\"");
Run Code Online (Sandbox Code Playgroud)

但是这种方法不能像我预期的那样工作.请告诉我如何更换字符串以符合上述服务器.

谢谢 :)

java regex json replaceall gson

3
推荐指数
1
解决办法
5557
查看次数

如果条件为假,则将单元格留空

其他问题也涉及到这一点,并提供了对非常大的数据集不可行的解决方案。我有一个跨 9 列的公式,如下所示:

=IF(A1=A2, B2, "zz") 
Run Code Online (Sandbox Code Playgroud)

然后我自动填充大约 350 万个单元格 + 复制 -> 粘贴值。然后我找到并用“空单元格”替换“zz”。然而,查找和替换一百万左右的“zz”字符串是一个非常缓慢的过程。我宁愿一开始就不在那里写任何东西。所以我的问题是,如何编写以下公式:

=IF(A1=A2, B2, 'leave cell alone and write nothing there at all')
Run Code Online (Sandbox Code Playgroud)

Excel 似乎无法在 FALSE(或 TRUE)的情况下不接触单元格。

excel if-statement find replaceall

3
推荐指数
1
解决办法
7万
查看次数

如何仅在 scala 中小写捕获组

这段代码:

\n\n
"""{"nAMe": "Deloise", "WINS": [["three of a kind", "5\xe2\x99\xa3"]]}""".replaceAll("""(\\"[^"]+\\" *:)""", "|UPERCASETEST|$1|".toLowerCase())\n
Run Code Online (Sandbox Code Playgroud)\n\n

生产:

\n\n
String = {|upercasetest|"nAMe":| "Deloise", |upercasetest|"WINS":| [["three of a kind", "5\xe2\x99\xa3"]]}\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我期待的时候:

\n\n
String = {|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5\xe2\x99\xa3"]]}\n
Run Code Online (Sandbox Code Playgroud)\n\n

知道为什么捕获组不希望小写以及如何解决它吗?

\n

regex scala replaceall

3
推荐指数
1
解决办法
308
查看次数

如何在一行中替换多个字符串?

我想在每个元音之前插入"OB".我试过下面的代码:

String out=txt.toUpperCase();

out=out.replaceAll("A","OBA");
out=out.replaceAll("E","OBE");
out=out.replaceAll("I","OBI");
out=out.replaceAll("O","OBO");
out=out.replaceAll("U","OBU");
out=out.replaceAll("Y","OBY");
Run Code Online (Sandbox Code Playgroud)

当我使用上面的代码时,它将替换AOBA,但是当它替换OOBO它时,将替换O原始文本和Oin OBA.例如,因为"I WON'T"我想要输出"OBI WOBON'T",但是它给出"OBOBI WOBON'T"O来自OBI第一行的from 被视为元音.

我需要一个解决方案,它不会取代O加密中的新内容.

java regex replaceall

3
推荐指数
1
解决办法
342
查看次数

使用 {} 大括号的 Java String replaceAll() 方法

因此,对于我在 Android Studio 中的应用程序,我想替换以下内容:

String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
            String replacedCard = card.replaceAll("{Player1}", "Poep");
}
Run Code Online (Sandbox Code Playgroud)

String 卡的一个例子可以是:{Player1} 与你旁边的人交换饮料。

不知何故,我无法使用 {} 进行替换。用 { 表示:“悬空元字符”。截图:https : //prnt.sc/s2bbl8

有解决方案吗?

java string replaceall

3
推荐指数
1
解决办法
337
查看次数

Groovy删除多行注释

如何使用groovy删除多行注释?

/* Use groovy replaceAll regex to 
   remove this comment */
Run Code Online (Sandbox Code Playgroud)

我从文件,文件对象中读入上述文本,然后将其转换为字符串.如果评论跨越一行,我可以使用下面发布的replaceAll方法将其删除:

def file = new File('myfile')
def fileString = file.getText()

println fileString.replaceAll('/\\* .* \\*/','')
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用(?m)标志,但我无法识别我的模式.我尝试了以下语句,但都无法匹配我的模式.

fileString.replaceAll('(?m)/\\* (.*) \\*/'    ,'')  #multiline switch
fileString.replaceAll('(/\\*)(.|\n\r)*(\\*/)' ,'')  #match all .* (include \n\r)
Run Code Online (Sandbox Code Playgroud)

我想过使用DotAll,最后是(\ s)和$ {}.但是,我不确定如何有效地将它混合到正则表达式中.任何帮助都会受到欢迎.谢谢.

regex groovy comments multiline replaceall

2
推荐指数
1
解决办法
4139
查看次数

每x个句子后打破一个字符串

我有一个很长的文本,我试着在每3个句子后打破它.

资源:

"Sentence 1. Sentence 2? Sentence 3! Sentence 4. Sentence 5. Sentence 6. Sentence 7. Sentence 8. Sentence 9. Sentence 10."

应该返回:

"Sentence 1. Sentence 2? Sentence 3! Sentence 4. Sentence 5. Sentence 6. Sentence 7. Sentence 8. Sentence 9. Sentence 10."

目前我的正则表达式(?<=[\.?!])\s匹配句子之间的所有空格.所以我可以使用它来拆分String然后迭代以添加换行符:

String[] splits = src.split(regex);
StringBuilder b = new StringBuilder();
int index = 0;
for (String s : splits) {
    if (index == 3) {
        b.append("\n");
        index = 0;
    } else if (index > …
Run Code Online (Sandbox Code Playgroud)

java regex android replaceall

2
推荐指数
1
解决办法
84
查看次数

使用 kotlin 字符串函数/字符串格式添加空格

这可能会在这里被问过几次......我想在字符串的每四个字符之间添加空格(8888319024981442)。我的字符串长度正好是 16。 String.format没有帮助

避免使用 split 或在内存中创建多个字符串。

有没有可以快速使用的 kotlin 函数/String.format。

string string.format replaceall kotlin

2
推荐指数
1
解决办法
1812
查看次数