使用java和Android提取bbcode引用但不提取引用标签内的内容 [英] Extracting bbcode quote using java and Android but not extracting the content within the quote tag
问题描述
我将使用引号提取 bbcode,但在实际输出到来时无济于事.
I am going to extract the bbcode with the quotes but to no avail when the actual output is coming.
我想实现 bbcode 解析模块,用于根据需要的输出提取引号.引号的数量应该是递归方法或其他..
I would like to implement the bbcode parsing module for extracting the quotes as desired output . The number of quotes shall be a recursive method or some else..
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[/网址]爱莎测试
Testing http://www.yourube.com?watch?v=asasdsadsa [url] aisa [/url] aisa Testing
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 Code 标签对的通用 Java 正则表达式:
This is the general Java regex to match pairs of BB Code tags:
\\[([^\\]]+)\\](.+?)\\[/\\1\\]
这将抓取顶级比赛,例如在[a][b] hi [/b] hello [/a][c] yo [/c]
中,第2组将匹配[b] hi [\b] hello
和 yo
.(此处演示)
This will grab top level matches e.g. in [a][b] hi [/b] hello [/a][c] yo [/c]
, group 2 will match [b] hi [\b] hello
and yo
. (Demonstrated here)
在我看来,任何正则表达式解决方案都需要您使用递归(在正则表达式之外)来查找所有匹配项.您将必须找到所有顶级匹配项(将它们添加到某个数组中),然后在每个匹配项上递归使用相同的正则表达式(将它们全部添加到相同的结果数组中),直到最终找不到更多匹配项.
Any regex solution is in my opinion going to require you to use recursion (outside of the regex) to find all matches. You're going to have to find all top level matches (add them to some array), then recursively use the same regex on each of the matches (adding them all to the same result array) until eventually no matches more matches can be found.
在该示例中,您可以看到您需要在 [b] hi [\b] hello
上再次运行正则表达式以返回 [b] hi [/b]
即 hi
.
In that example you can see you'd need to then run the regex again on [b] hi [\b] hello
to return the content of the [b] hi [/b]
which is hi
.
例如,对于输入:
[A] outer [B] [C] last one left [/C] middle [/B] [/A] [A] out [B] in [/B] [/A]
首先,针对该字符串运行正则表达式并查看第 2 组匹配项:
First you, run the regex against that string and look at the group 2 matches:
outer [B] [C] last one left [/C] middle [/B]
out [B] in [/B]
将这些添加到结果数组中,然后针对这些匹配运行正则表达式并获得:
Add those to the result array, then you run the regex against those matches and get:
[C] last one left [/C] middle
in
将这些添加到结果数组中,然后再次针对这些匹配运行它并得到:
Add those to the result array, and again run it against those matches and get:
last one left
[no matches]
最后,您将针对 最后一个左
运行它,并且没有更多匹配项,所以您完成了.
And finally you'd run it against last one left
and get no more matches, so you're done.
Raju,如果您不熟悉递归,那么此时停止阅读并尝试自己解决问题对您非常有益 - 如果您放弃,请回来.也就是说...
这个问题的Java解决方案是:
A Java solution to this problem is:
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
}
}
ideone 输出:
[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
这篇关于使用java和Android提取bbcode引用但不提取引用标签内的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!