我将用引号提取bbcode,但在实际输出时没有用。
我想实现bbcode解析模块,用于提取所需输出的引号。引号应为递归方法或其他方法。。
INput :
Testing [quote]http://www.yourube.com?watch?v=asasdsadsa [url] aisa [/url] [/quote] Testing
Desired Output
测试http://www.yourube.com?watch?v=asasdsadsa[url]aisa[/url]爱莎测试
Actual Output:
http://www.yourube.com?watch?v=asasdsadsa [url] aisa [/url]
http://www.yourube.com?watch?v=asasdsadsa aisa
下面是我的代码
String s = "[quote]http://www.yourube.com?watch?v=asasdsadsa [url] aisa [/url][/quote]";
String t = bbcode(s);
System.out.println(t);
String u = bbcode2(t);
System.out.println(u);
public static String bbcode(String text) {
String html = text;
HashMap<String,String> bbMap = new HashMap<String , String>();
bbMap.put("\[quote\](.+?)\[/quote\]", "$1");
for (Map.Entry entry: bbMap.entrySet()) {
html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
}
return html;
}
public static String bbcode2(String text) {
String html = text;
HashMap<String,String> bbMap = new HashMap<String , String>();
bbMap.put("\[quote\](.+?)\[/quote\]", "$1");
bbMap.put("\[url\](.+?)\[/url\]", "$1");
for (Map.Entry entry: bbMap.entrySet()) {
html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
}
return html;
}
这是匹配BB代码标记对的通用Java正则表达式:
\[([^\]]+)\](.+?)\[/\1\]
这将获取顶级匹配,例如在[a][b] hi [/b] hello [/a][c] yo [/c]
中,组2将匹配[b] hi [b] hello
和yo
。(此处演示)
在我看来,任何正则表达式解决方案都需要使用递归(正则表达式之外)来查找所有匹配项。您必须找到所有顶级匹配(将它们添加到某个数组),然后在每个匹配上递归地使用相同的regex(将它们全部添加到同一结果数组),直到最终找不到匹配为止。
在该示例中,您可以看到您需要在[b] hi [b] hello
上再次运行regex,以返回[b] hi [/b]
的内容,即hi
。
例如,对于以下项的输入:
[A] outer [B] [C] last one left [/C] middle [/B] [/A] [A] out [B] in [/B] [/A]
首先,针对该字符串运行regex,并查看组2匹配项:
outer [B] [C] last one left [/C] middle [/B]
out [B] in [/B]
将这些添加到结果数组中,然后根据这些匹配项运行regex,得到:
[C] last one left [/C] middle
in
将这些添加到结果数组中,然后根据这些匹配再次运行它,得到:
last one left
[no matches]
最后,你将与last one left
比赛,不再有比赛,所以你完成了。
Raju,如果你不熟悉递归,那么在这一点上停止阅读并尝试自己解决问题对你来说是非常有益的——如果你放弃了,就回来吧。也就是说
这个问题的Java解决方案是:
public static void getAllMatches(Pattern p, String in, List<String> out) {
Matcher m = p.matcher(in); // get matches in input
while (m.find()) { // for each match
out.add(m.group(2)); // add match to result array
getAllMatches(p, m.group(2), out); // call function again with match as input
}
}
这里有一个关于表意的工作示例
视频输出:
[A]outer[B][C]last one left[/C]middle[/B][/A] [A]out[B]in[/B][/A]
-----------
- outer[B][C]last one left[/C]middle[/B]
- [C]last one left[/C]middle
- last one left
- out[B]in[/B]
- in
[quote]http://www.yourube.com?watch?v=asasdsadsa [url]aisa[/url] [/quote]
-----------
- http://www.yourube.com?watch?v=asasdsadsa [url]aisa[/url]
- aisa
不是最整洁的方式,而是非reg ex方式。。。
int lastIndex = 0;
String startString = "[quote]";
String endString = "[/quote]";
int start;
int end;
while (lastIndex != -1) {
start = string.indexOf(startString, lastIndex);
lastIndex = start;
if (lastIndex == -1) {
break;
}
end = string.indexOf(endString, lastIndex);
lastIndex = end;
if (lastIndex == -1) {
break;
}
System.out.println(string.substring(
start + startString.length,
end + 1));
}