String foo = "a3#4#b";
String afterPunctutationRemoval = foo.replaceAll("[,.;:?!'-_\"/()\\{}]", "");
System.out.println(afterPunctutationRemoval);
Run Code Online (Sandbox Code Playgroud)
它给了我一个"## b"的结果,有人可以解释我为什么吗?
它不应该按原样返回字符串吗?
我正在尝试使用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不会将替换文本视为自己被替换的候选者,而是快速地向前跳过.
这有一个简单的解决方案吗?
我正在创建json字符串以发送到服务器.我使用GSon生成json字符串.因为我的服务器不理解的字符串,如:\"key\":value但"key":value还是"a\/b"不过a/b.所以在生成json字符串之后,我使用replaceAll方法来解决这个问题:
String res = original.replaceAll("\\/", "/").replaceAll("\\"", "\"");
Run Code Online (Sandbox Code Playgroud)
但是这种方法不能像我预期的那样工作.请告诉我如何更换字符串以符合上述服务器.
谢谢 :)
其他问题也涉及到这一点,并提供了对非常大的数据集不可行的解决方案。我有一个跨 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)的情况下不接触单元格。
这段代码:
\n\n"""{"nAMe": "Deloise", "WINS": [["three of a kind", "5\xe2\x99\xa3"]]}""".replaceAll("""(\\"[^"]+\\" *:)""", "|UPERCASETEST|$1|".toLowerCase())\nRun Code Online (Sandbox Code Playgroud)\n\n生产:
\n\nString = {|upercasetest|"nAMe":| "Deloise", |upercasetest|"WINS":| [["three of a kind", "5\xe2\x99\xa3"]]}\nRun Code Online (Sandbox Code Playgroud)\n\n当我期待的时候:
\n\nString = {|upercasetest|"name":| "Deloise", |upercasetest|"wins":| [["three of a kind", "5\xe2\x99\xa3"]]}\nRun Code Online (Sandbox Code Playgroud)\n\n知道为什么捕获组不希望小写以及如何解决它吗?
\n我想在每个元音之前插入"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)
当我使用上面的代码时,它将替换A为OBA,但是当它替换O为OBO它时,将替换O原始文本和Oin OBA.例如,因为"I WON'T"我想要输出"OBI WOBON'T",但是它给出"OBOBI WOBON'T"了O来自OBI第一行的from 被视为元音.
我需要一个解决方案,它不会取代O加密中的新内容.
因此,对于我在 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
有解决方案吗?
如何使用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)和$ {}.但是,我不确定如何有效地将它混合到正则表达式中.任何帮助都会受到欢迎.谢谢.
我有一个很长的文本,我试着在每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) 这可能会在这里被问过几次......我想在字符串的每四个字符之间添加空格(8888319024981442)。我的字符串长度正好是 16。 String.format没有帮助
避免使用 split 或在内存中创建多个字符串。
有没有可以快速使用的 kotlin 函数/String.format。