java中使用正则表达式获取字符串数组中含有字符[ ]的字符串,将得到的内容用字符串term表示
1个回答
推荐于2016-03-22
展开全部
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JButtonTest
{
public static void main ( String[] args )
{
String input = "abc[1]dd[a-b]df[3.b]";
String regex = "\\[[^\\[\\]]*\\]";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (input);
while (matcher.find ())
{
System.out.println (matcher.group ());
}
}
}
更多追问追答
追答
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JButtonTest
{
public static void main ( String[] args )
{
String input = "安全*[5.1]sdfs安全管理[5.6.1]";
String regex = "([\u4e00-\u9fff]+)[^\\[\\]]*\\[[^\\[\\]]*\\]";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (input);
while (matcher.find ())
{
System.out.println (matcher.group (1));
}
input = "安全*[5.1]";
System.out.println (input.replaceAll (regex, "$1"));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询