Input string:
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# and #match this also# 这两条都移至最近一段时期(......)。 基本上说来,所有东西都是#。 应移至左边最接近的时期。
RegEx和Juan String能否做到这一点?
This most basic RegEx to match #anything# is this:
#(.*?)#
除此之外,我还遇到困难。
Edit:你不必告诉我如何撰写完整的方案。 我只需要一个充分的“快车道”解决办法,然后,我自行尝试操纵。
这里,我的解决办法源自<<>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("(\. |\. |\?|\])").matcher(paragraph.toString());
return m.replaceAll("$1<br /><br />");
}
这给我带来了预期产出。 但有一个问题: 如果在<>#>之间有一段时期 (例:#Mrs. Smith kicks Ms. Smith in the relevantsite<#/em>), the input.split(>/>);
Line>,将打破第__#。 因此,我将将<条码>输入.split()改为“Ex”。