From dd2b4467ed6b5659989644b5b170dc66412b3de8 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 11 Feb 2023 23:57:56 +0100 Subject: [PATCH] new StringUtil.asPatternInSentense method --- .../fr/pandacube/lib/util/StringUtil.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java index 1777e95..d87ef3c 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java @@ -139,4 +139,24 @@ public class StringUtil { return s -> Long.toString(operator.apply(Long.parseLong(s))); } + + /** + * Generate a {@link Pattern} with extra wrapping regex around the provided one to consider a sentense (like a chat + * message). For instance, the returned pattern will only match the expression at the beginning or end of sentence, + * or separated by the rest of it with space or another non-letter character. + * @param wordPattern the regex pattern to wrap. + * @param caseInsensitive if the pattern must match ignoring case. + * @return a {@link Pattern}. The matching will match 3 groups. The first group is the eventual non-letter separator + * before the matched word, the second one is the actual word, and the last one is the eventual non-letter separator + * after the matched word. Any additionnal pattern group between the 2nd and the last one are thoses provided in the + * wordPattern. + */ + public static Pattern asPatternInSentense(String wordPattern, boolean caseInsensitive) { + return Pattern.compile((caseInsensitive ? "(?i)" : "") + "(\\P{L}|^)(" + wordPattern + ")(\\P{L}|$)"); + } + + + + + }