BTSAE[3][REAL_REAK_BTSAE_404] ,要写个正则表达式提取第一个[]之间的数字出来,急啊

解决方案 »

  1.   


    String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
    String reg = "^.*?\\[([^\\[\\]]*)\\].*?$";
    System.out.println(str.replaceAll(reg, "$1"));
      

  2.   


    /**
     * create date:2009-6-12 author:Administrator
     * 
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String type = "BTSAE[3][REAL_REAK_BTSAE_404]";
    Pattern p = Pattern.compile("([\\d]{1,3})"); // System.out.println("the type is.." + type);
    Matcher m = p.matcher(type);
    if (m.find()) {
    System.out.println(m.group()); }
    }
      

  3.   

    http://edu.yesky.com/edupxpt/18/2143018.shtmlJava正则表达式详解
      

  4.   

    随便写下
    String type = "BTSAE[3][REAL_REAK_BTSAE_404]";
    Pattern p = Pattern.compile("\\[[1-9]+(\\d)*\\]");
    Matcher m = p.matcher(type);
    if (m.find()) {
        String ret = m.group();
        if (!ret.equals(""))
            System.out.println(ret.substring(1, ret.length() - 1)); 
      

  5.   

    补充:想要0就这样
    Pattern p = Pattern.compile("\\[[[1-9]+(\\d)*|0]\\]");
      

  6.   


    String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
    String reg="^.*?\\[(\\d+)\\].*?$";
    System.out.println(str.replaceAll(reg, "$1"));
      

  7.   


    package com.ricky.www;/*
    * BTSAE[3][REAL_REAK_BTSAE_404] ,要写个正则表达式提取第一个[]之间的数字出来,急啊
    */
    public class Test{
    public static void main(String[] args){
    String message = "BTSAE[313543][REAL_REAK_BTSAE_404]";
    String regex = "[^\\[]*\\[(\\d+).*";
    String result = message.replaceAll(regex,"$1");
    System.out.println(result);
    }
    }
      

  8.   

    希望是用正则表达式能够直接提取,不要用replaceAll方式完成。         Pattern pattern = Pattern.compile("([\\d+]{1,3})");
            Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
            if(m.find()){
                System.out.println(m.group());
            }
    楼上的这种写法好像对[]里面的数字个数有限制,{1,3}只能取到前3位,希望是只要第一个[]是数字就提取出来,不管数字的有多少位
      

  9.   

    LZ 不引用,还没发现这个是错的
    试试 BTSAE[3+3][REAL_REAK_BTSAE_404]这个应该对Pattern pattern = Pattern.compile("(?<=\\[)\\d+(?=\\])"); 
    Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]"); 
    if(m.find()){ 
    System.out.println(m.group()); 
    }
      

  10.   

    谢谢楼上的兄弟,不过还有点缺陷。如果BTSAE[][343][REAL_REAK_BTSAE_404],就不对了,一定是匹配第一个[]的,
    如果字符串为BTSAE[][343][REAL_REAK_BTSAE_404]就把第二个[]的值取出来。兄弟再帮忙想想
    Pattern pattern = Pattern.compile("(?<=\\[)\\d+(?=\\])"); 
    Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]"); 
    if(m.find()){ 
        System.out.println(m.group()); 
    }
      

  11.   

    不会吧
    对 BTSAE[][343][REAL_REAK_BTSAE_404]
    我这里的结果是 343(?<=\\[)\\d+(?=\\])(?<=\\[) 断言 \\d+ 前面的是 [
    \\d+ 匹配至少一个数字
    (?=\\]) 断言 \\d+ 后面的是 ]理论和实际上都不会匹配 []
      

  12.   

    我的意思好像有点没有表达清楚,不应该用匹配第一个[]来描述, 这个正则表达式的功能只用于提取第一个[]里面的数字,请注意是第一个[],而不是第一个出现数字的[],当第一个[]不是数字是,不满足条件就可以了,不需要再去寻找下一个满足条件的[]。举例 
    BTSAE[][343][REAL_REAK_BTSAE_404]  期望 m.find() 执行结果为false就可以了BTSAE[1+3][343][REAL_REAK_BTSAE_404]  期望 m.find() 执行结果为false就可以了BTSAE[4567][343][REAL_REAK_BTSAE_404]  期望 m.find() 执行结果为true
      

  13.   

    再补充一下BTSAE[4567][343][REAL_REAK_BTSAE_404]  期望 m.find() 执行结果为true,并能m.group()直接返回4567
    麻烦楼上的高人帮忙解答,感谢
      

  14.   

    正在学习正则中,不知道下面的代码能不能满足楼主的要求:import java.util.regex.*;
    public class RegTest{
    public static void main(String[] args){
    String s="BTSAE[4567][343][REAL_REAK_BTSAE_404]";
    Pattern p=Pattern.compile("BTSAE\\[(\\d+)\\].");
    Matcher m=p.matcher(s);
    System.out.println(m.find());
    System.out.println(m.group(1));
    }
    }
      

  15.   

    Pattern pattern = Pattern.compile("(?<=\\[)[^\\[\\]]*(?=\\])");
    Matcher m = pattern.matcher("BTSAE[-343][REAL_REAK_BTSAE_404]");
    if (m.find()) {// if 取第一个,while 取全部
    if(Pattern.matches("^-?\\d+$", m.group())){
    System.out.println(m.group());// 如果是整数则打印
    }else{
    System.out.println(false);// 不是整数,打印 false
    }
    }