这是Java中这样的字符串。
String string="abc$[A]$def$[B]$ghi";
我想搜索位于$[*]$
模式中的单词。上面字符串的结果是A
, B
。
String s = "abc$[A]$def$[B]$ghi";
Pattern p = Pattern.compile("\\$\\[.*?\\]\\$");
Matcher m = p.matcher(s);
while(m.find()){
String b = m.group();
System.out.println(">> " +b.substring(2, b.length()-2));
}
使用正则表达式。在 Java 中,您可以使用Pattern 类。
您可以为此使用正则表达式。看看Pattern和Matcher类。
在这种情况下,您将使用的正则表达式是:
\$\[.*?\]\$
或者,您可以使用String.indexOf和String.substr。