gpt4 book ai didi

Java RegEx 乐趣 - 玩转句子

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

输入字符串:

Lorem ipsum tip. Lorem ipsum loprem ipsum septum #match this#, lorem ipsum #match this too#. #Do not match this because it is already after a period#.

期望的输出:

Lorem ipsum tip. #match this# #match this too# Lorem ipsum loprem ipsum septum, lorem ipsum. #Do not match this because it is already after a period#.

请注意,#match this##match this too# 均已移至最近的句点 (.) 旁边。基本上,## 中的所有内容都应移至左侧最近的句点。

RegEx 和 Java 字符串处理可以完成这个任务吗?

匹配#anything#的最基本的正则表达式是这样的:

\#(.*?)\#

除此之外我还遇到困难。

编辑:您不必告诉我如何编写完整的程序。我只需要一个足够的正则表达式解决方案,然后我将尝试自己的字符串操作。

这是我的解决方案,源自 glowcoder 的答案:

public static String computeForSlashline(String input) {

String[] sentences = input.split("\\.");

StringBuilder paragraph = new StringBuilder();
StringBuilder blocks = new StringBuilder();

Matcher m;

try {

// Loop through sentences, split by periods.
for (int i = 0; i < sentences.length; i++) {

// Find all the #____# blocks in this sentence
m = Pattern.compile("(\\#(.*?)\\#)").matcher(sentences[i]);

// Store all the #____# blocks in a single StringBuilder
while (m.find()) {

blocks.append(m.group(0));

}

// Place all the #____# blocks at the beginning of the sentence.
// Strip the old (redundant) #____# blocks from the sentence.
paragraph.append(blocks.toString() + " " + m.replaceAll("").trim() + ". ");

// Clear the #____# collection to make room for the next sentence.
blocks.setLength(0);

}

} catch(Exception e) { System.out.println(e); return null; }

// Make the paragraph look neat by adding line breaks after
// periods, question marks and #_____#.
m = Pattern.compile("(\\. |\\.&nbsp;|\\?|\\])").matcher(paragraph.toString());

return m.replaceAll("$1<br /><br />");

}

这给了我想要的输出。然而,有一个问题:如果 #__# 之间有一个句点(例如:#Mrs. Smith 在敏感点踢了 Smith 女士#),输入.split("\\."); 行将分解 #__#。因此,我将用 RegEx 替换 input.split() 行。

最佳答案

我将使用的骨架如下:

String computeForSlashline(String input) {

String[] sentences = input.split("\.");
for(int i = 0; i < sentences.length; i++) {
// perform a search on each sentence, moving the #__# to the front
}
StringBuilder sb = new StringBuilder();
for(String sentence : sentences) {
sb.append(sentence).append(". ");
}
return sb.toString().trim();

}

关于Java RegEx 乐趣 - 玩转句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7340864/

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