gpt4 book ai didi

JAVA 正则表达式 : Making custom regex for String. split()

转载 作者:行者123 更新时间:2023-12-02 04:30:31 26 4
gpt4 key购买 nike

我需要以仅忽略这些标点符号的方式分割字符串:

(.)、(,)、(!) 和 (?)

所以如果我有这个字符串:

String a = "I can't split this string! Guys, can you help me? Thanks in advance."

我有这个正则表达式(它没有帮助):

String.split("[\\p{Punct}\\s]+");

它的输出是:

IcantsplitthisstringGuyscanyouhelpmeThanksinadvance

如果您注意到 can't 被拆分,因为撇号是作为标点符号给出的,而我不想这样。

最佳答案

  public static void main(String[] args)
{
String a = "I can't split this string! Guys, can you help me? Thanks in advance.";

String[] splitted = a.split("[.,!?\\s]+");
for (String s : splitted)
{
System.out.println(s);
}
}

如果您不想用空格分隔,请删除 \\s

关于JAVA 正则表达式 : Making custom regex for String. split(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543106/

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