gpt4 book ai didi

java - 正则表达式不喜欢 out#

转载 作者:行者123 更新时间:2023-12-02 03:41:13 25 4
gpt4 key购买 nike

我编写以下代码来删除文本中的所有主题标签单词:

public static void main(String[] args) {

System.out
.println(removeHashtag("Got an infection in my eye. Pharmacist thinks something bitten me. This wouldn't have happened under Simeone. Wenger a#sarcasm #wengerin"));

}

public static String removeHashtag(String commentstr) {
String arrWord[] = commentstr.split(" ");
String sentenceWithoutHash = commentstr;
System.out.println(sentenceWithoutHash);
for (int i = 0; i < arrWord.length; i++) {
if (arrWord[i].contains("#")) {
String regex = "\\s*\\" + arrWord[i] + "\\b\\s*";
sentenceWithoutHash = sentenceWithoutHash.replaceAll(regex, "");
}
}
return sentenceWithoutHash;
}

但是此代码不适用于此文本

Got an infection in my eye. Pharmacist thinks something bitten me. This wouldn't have happened under Simeone. Wenger out#sarcasm #wengerin"

看来正则表达式不喜欢out#

有人可以帮忙吗?

最佳答案

您可以使用此正则表达式删除任何包含 # 的单词:

String rep = str.replaceAll("\\s*\\w*#\\w*\\s*", "");

RegEx Demo

关于java - 正则表达式不喜欢 out#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814496/

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