我遵循了这个方法下面的解决方案,但当涉及到在代码之间提取字符串时,正则表达式模式中存在错误,或者模式无法匹配字符串的部分
如
这是一个测试
[url] http://www.google.com.hk [/url]
和[img] http://www.abc.com/test.png [/img]
如何在java中获取引号之间的数据?
您没有说明您是否有嵌套模式,所以这里有一个例子来启动您。
这里需要对\
进行双转义,因为也是字符串的转义符。
String s = "This is a test [url] http://www.google.com.hk [/url]n"
+ " and [img] http://www.abc.com/test.png [/img]";
Pattern p = Pattern.compile("\[[^\]]*\]([^\]]*)\[[^\]]*\]");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1).trim());
}
参见working demo
正则表达式:
[ '['
[^]]* any character except: ']' (0 or more times)
] ']'
( group and capture to 1:
[^]]* any character except: ']' (0 or more times)
) end of 1
[ '['
[^]]* any character except: ']' (0 or more times)
] ']'
如果您希望特定于img
或url
代码标记,可以使用以下内容。
Pattern p = Pattern.compile("(?i)\[(?:url|img)\]([^\]]*)\[\/(?:url|img)\]");
这里有一个regex,它假设标记的名称("bbcodes")仅由单词字符组成。它还检查结束标记的名称是否与开始标记的名称匹配。(这是通过引用backrefence 1
的打开标签的名称来完成的。)
[(w+)](.+?)[/1]
^^^ ^^^
1 2
背景参考:
- 打开标记的名称。(例如
url
) - 开头和结尾标记之间所有字符的非贪婪匹配
这是一个演示。(另请参阅现场演示。)
import java.util.*;
import java.util.regex.*;
class RegexTester
{
public static void main (String[] args)
{
String s =
"This is a test [url] http://www.google.com.hk [/url]n"
+ " and [img] http://www.abc.com/test.png [/img]";
Pattern p = Pattern.compile("\[(\w+)\](.+?)\[/\1\]");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println("Match=[" + m.group(2).trim() + "]");
}
}
}
预期结果
Match=[http://www.google.com.hk]
Match=[http://www.abc.com/test.png]