gpt4 book ai didi

java - 如何使用java中的Pattern删除字符串中的@

转载 作者:行者123 更新时间:2023-12-02 05:33:20 25 4
gpt4 key购买 nike

我需要删除以@开头的字符串的一部分。

我的示例代码适用于一个字符串,但适用于另一个字符串则失败。

失败一:无法删除@news4buffalo:

String regex = "\\@\\w+ || @\\w*";
String rawContent = "RT @news4buffalo: Police say a shooter fired into a crowd yesterday on the Oakmont overpass, striking and killing a 14-year-old. More: http…";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(rawContent);
if (matcher.find()) {
rawContent = rawContent.replaceAll(regex, "");
}

成功一:

String regex = "\\@\\w+ || @\\w*";
String rawContent = "@ZaslowShow couldn't agree more. Good crowd last night. #LetsGoFish";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(rawContent);
if (matcher.find()) {
rawContent = rawContent.replaceAll(regex, "");
}

输出:

couldn't agree more. Good crowd last night. #LetsGoFish

最佳答案

从您的问题看来,这个正则表达式可以为您工作:

rawContent = rawContent.replaceAll("@\\S*", "");

关于java - 如何使用java中的Pattern删除字符串中的@,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292270/

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