gpt4 book ai didi

Java,正则表达式 : Regex to find hashtags slowing down server till it crashes

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

我正在开发一个 Spring-MVC 应用程序,其中当用户使用 # 输入注释时,它们会被扫描并添加为标签。理想情况下,正常情况下应该不会有任何问题。但是当我在服务器上部署代码时,每次迭代后,服务器开始变慢。在 2-3 次迭代中,服务器实际上停止了并且没有服务任何请求。删除该功能会使一切正常工作。到目前为止没有错误。不幸的是,本地主机上没有问题。

Postgresql进程日志:

postgres 27916 22320  0 11:16 ?        00:00:00 postgres: postgres Person2 127.0.0.1(41936) idle                                                                            
postgres 30634 22320 2 13:05 ? 00:00:05 postgres: postgres Person2 127.0.0.1(52105) idle
postgres 30718 22320 1 13:05 ? 00:00:03 postgres: postgres Person2 127.0.0.1(52314) idle
postgres 30719 22320 1 13:05 ? 00:00:03 postgres: postgres Person2 127.0.0.1(52315) UPDATE waiting
postgres 30720 22320 0 13:05 ? 00:00:02 postgres: postgres Person2 127.0.0.1(52316) INSERT waiting
postgres 30721 22320 1 13:05 ? 00:00:02 postgres: postgres Person2 127.0.0.1(52317) idle
postgres 30722 22320 1 13:05 ? 00:00:02 postgres: postgres Person2 127.0.0.1(52318) UPDATE waiting
postgres 30835 22320 0 13:05 ? 00:00:01 postgres: postgres Person2 127.0.0.1(52512) idle in transaction
postgres 30836 22320 1 13:05 ? 00:00:02 postgres: postgres Person2 127.0.0.1(52520) INSERT waiting

有问题的代码,主要调用:

  List<String> totalTags = this.groupNotesService.findHashTags(Jsoup.parse(commentText).text());
if((totalTags!=null)&&(!totalTags.isEmpty())){
for(String allTags : totalTags){
boolean tagExists = this.groupNotesService.checkIfHashTagAlreadyPartOfTags(allTags,groupNotes.getTags());
if(!tagExists){
change = true;
groupNotes.setTags(groupNotes.getTags()+","+allTags);
}
}
}
if(change){
this.groupNotesService.directUpdateGroupNote(groupNotes);
}

用于查找主题标签的代码和正则表达式:

 @Override
public List<String> findHashTags(String text){
if(text == null){
return new ArrayList<>();
}

Set<String> sortedTags = new HashSet<>();
List<String> processedTags = new ArrayList<>();
Pattern pattern = Pattern.compile("#\\b.*?\\b#|\\B#\\w+");
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
String outString = matcher.group();
outString = outString.replace("#","");
outString = outString.replace(",","");
sortedTags.add(outString);
}
processedTags.addAll(sortedTags);

return processedTags;
}

检查现有标签:

  @Override
public boolean checkIfHashTagAlreadyPartOfTags(String tagToCheck, String tags){
if((tagToCheck==null)||(tagToCheck.isEmpty())){
return true;
}
if(tags == null){
return false;
}
tags = tags.toLowerCase();
tagToCheck = tagToCheck.toLowerCase();
String[] tagsSplit = tags.split(",");
for(String tag : tagsSplit){
if(tag.equals(tagToCheck)){
return true;
}
}
return false;
}

这里出了什么问题?有任何想法吗。谢谢。

最佳答案

您的原始正则表达式包含以下文本:

some #hashtags #enclosed in sentences#

匹配完整句子#hashtags #enheld in statements#,但不匹配#enlined

此外,如果给出的文本不太长(超过 1000 个字符),并且在文本开头有大量正常的 #hashtags,则正则表达式的表现会很差:

#Lorem #ipsum #dolor #sit #amet, #consectetur #adipiscing #elit, #sed #eiusmod #tempor #incidunt #ut #labore #et #dolore #magna #aliqua. #Ut #enim #ad #minim #veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

如下所示:https://regex101.com/r/pMf9YT/1

35676 步中的 23 场比赛。

如果将正则表达式稍微修改为 #\b[^#]+\b#|\B#\w+ ,它会匹配 #hashtags 和句子 #包含在句子中#。并且表现得更好:https://regex101.com/r/xHuxpI/2

1767 步中的 23 场比赛。

尽管这是一种改进,但可能不是罪魁祸首。

关于Java,正则表达式 : Regex to find hashtags slowing down server till it crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277667/

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