gpt4 book ai didi

java - 返回文本中给定位置前后指定数量的单词

转载 作者:行者123 更新时间:2023-11-29 05:17:09 26 4
gpt4 key购买 nike

我对以下代码有一个大问题。我希望它在找到的关键字(针)之前和之后返回 n 个单词,但它从来没有。

如果我有文字,说

"There is a lot of interesting stuff going on, when someone tries to find the needle in the haystack. Especially if there is anything to see blah blah blah". 

我有这个正则表达式:

"((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}\b)needle(\b(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5})"

这是否应该不完全匹配给定字符串中的针并将文本返回为

someone tries to find the needle in the haystack. Especially if

它从来没有 :-( 在执行时,我的方法总是返回一个空字符串,虽然我很清楚,关键字在给定的文本中。

private String trimStringAtWordBoundary(String haystack, int wordsBefore, int wordsAfter, String needle) {
if(haystack == null || haystack.trim().isEmpty()){
return haystack ;
}

String textsegments = "";

String patternString = "((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,"+wordsBefore+"}\b)" + needle + "(\b(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,"+wordsAfter+"})";


Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(haystack);

logger.trace(">>> using regular expression: " + matcher.toString());

while(matcher.find()){
logger.trace(">>> found you between " + matcher.regionStart() + " and " + matcher.regionEnd());
String segText = matcher.group(0); // as well tried it with group(1)
textsegments += segText + "...";
}

return textsegments;
}

很明显,问题出在我的正则表达式中,但我无法弄清楚它有什么问题。

最佳答案

您的正则表达式基本上没问题,但在 Java 中您需要转义 \b:

"((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}\\b)needle(\\b(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5})"

关于java - 返回文本中给定位置前后指定数量的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129384/

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