求一个正则表达式提取中括号里的内容

weixin_38663051 2017-05-05 07:30:08
求一个正则表达式,提取两个中括号里的内容

格式为两个中括号“[xxxxx][xxxxx]”,我想要提取两个中括号里的内容
比如字符串:[BF0001][交易失败]

我想提取出“BF0001” 和 “交易失败”

请问正则该怎么写



...全文
11211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wowpH 2019-08-09
  • 打赏
  • 举报
回复
引用 4 楼 mmqw 的回复:
		String content = "[BF0001][交易失败]";
String reg = "\\[(.*?)\\]";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(content);

while (matcher.find())
{
String src = matcher.group(1);
System.out.println(src);
}

刚好满足我的要求
白夜布衣 2019-08-09
  • 打赏
  • 举报
回复
\[(.*?)\]
wink-172 2017-12-14
  • 打赏
  • 举报
回复
可以的
引用 1 楼 genaro26 的回复:

public static List<String> getData(String data){  
          
        List<String> list=new ArrayList<String>();  
        Pattern p = Pattern.compile("(\\[[^\\]]*\\])");  
        Matcher m = p.matcher(data);  
        while(m.find()){  
            list.add(m.group().substring(1, m.group().length()-1));  
        }  
        return list;  
    }
这样就可以了,我试过了
雾里看花の 2017-06-08
  • 打赏
  • 举报
回复

public static List<String> getData(String data){  
          
        List<String> list=new ArrayList<String>();  
        Pattern p = Pattern.compile("(\\[[^\\]]*\\])");  
        Matcher m = p.matcher(data);  
        while(m.find()){  
            list.add(m.group().substring(1, m.group().length()-1));  
        }  
        return list;  
    }
这样就可以了,我试过了
mmqw 2017-06-08
  • 打赏
  • 举报
回复
		String content = "[BF0001][交易失败]";
		String reg = "\\[(.*?)\\]";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(content);

		while (matcher.find())
		{
			String src = matcher.group(1);
			System.out.println(src);
		}
mmqw 2017-06-08
  • 打赏
  • 举报
回复
[(.*?)]
String content = "[BF0001][交易失败]";
		String reg = "[(.*?)]";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(content);

		while (matcher.find())
		{
			String src = matcher.group(1);
			System.out.println(src);
		}
  • 打赏
  • 举报
回复
引用 1 楼 genaro26 的回复:

public static List<String> getData(String data){  
          
        List<String> list=new ArrayList<String>();  
        Pattern p = Pattern.compile("(\\[[^\\]]*\\])");  
        Matcher m = p.matcher(data);  
        while(m.find()){  
            list.add(m.group().substring(1, m.group().length()-1));  
        }  
        return list;  
    }
这样就可以了,我试过了

81,123

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧