字符串格式如下
[1234] hello
[56789] world
[112233] !!!!难道要用类似下面的方法吗 
switch 
case '['
case ']'
substring有简单的方法没 
正则表达式 \\[(\\d+?)\\]    \\d+?    \\d*都不行啊

解决方案 »

  1.   

            Pattern numbers = Pattern.compile("(\\d+)");
            Matcher matcher = numbers.matcher("[124] hello");
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
      

  2.   

    public class Test {
    public static void main(String[] args) {
    String s = "[56789] world";
    String[] str = s.split("]");
    String target = "";
    if(str.length != 0) {
    target = str[0].substring(1);
    }
    System.out.println(target);
    }
    }
    不知道符合楼主 要求不
      

  3.   

    这个方法不错,但是在执行s.split("]")方法的时候必须判断s是否为Null,要不会报异常信息..
      

  4.   


    \\d+一样可以,关键在那个while(matcher.find()) 
    就是尝试去寻找匹配的字符串,找到了就打印出来。
      

  5.   

    o(︶︿︶)o 唉  我把我在做的东西说下吧,,,看看各位有啥高见我在用java把酷狗的krc歌词转成lrc格式的   krc是加密过的,,,,现在经过几步后能转成下面的格式、
    [2372] Le ciel obscure
    [2949] La solitude qui nous rend la peine
    [5603] La ceour brise
    [6714] a cause qu’il y a vécu seul
    [10256] L’amour est parti
    [11372] il y a longtemp que je t'ai vu
    [13124] C'est trop long
    [14544] C'est incroyable que je peux vivre comme ca...
    [20704] 阿桑 - 寂寞在唱歌
    [25047] 天黑了
    [27611] 孤独又慢慢割着
    [31359] 有人的心又开始疼了
    [38751] 爱很远了很久没再见了
    [44835] 就这样竟然也能活着
    [51140] 你听寂寞在唱歌
    [54047] 轻轻的 狠狠的
    [56327] 歌声是这么残忍
    [60571] 让人忍不住泪流成河
    [62803] 谁说的
    [77766] 人非要快乐不可
    [81166] 好像快乐由得人选择
    [88744] 找不到的那个人来不来呢
    [95534] 我会是谁的谁是我的
    [101598] 你听寂寞在唱歌
    [104526] 轻轻的 狠狠的
    [106394] 歌声是这么残忍
    [111118] 让人忍不住泪流成河
    [115126] 你听寂寞在唱歌
    [118260] 温柔的 疯狂的
    [120089] 悲伤越来越深刻
    [124115] 怎样才能够让它停呢
    [130004] 寂寞在唱歌
    [137193] 专辑:寂寞在唱歌
    [139049] 演唱:阿桑
    [154027] 你听寂寞在唱歌
    [156925] 轻轻的 狠狠的
    [159339] 歌声是这么残忍
    [163521] 让人忍不住泪流成河
    [165742] 你听寂寞在唱歌
    [171025] 温柔的 疯狂的
    [174089] 悲伤越来越深刻
    [177304] 怎样才能够让它停呢
    [180928] 你听寂寞在唱歌
    [184192] 轻轻的 狠狠的
    [185917] 歌声是这么残忍
    [189376] 让人忍不住泪流成河
    [194516] 你听寂寞在唱歌
    [196906] 温柔的 疯狂的
    [199513] 悲伤越来越深刻
    [202532] 谁能帮个忙让它停呢
    [206400] HE~
    [215096] 天黑得
    [223616] 像不会再天亮了
    [225933] 明不明天也无所谓了
    [230131] 就静静的看青春难依难舍
    [239039] 泪还是热的泪痕冷了
    [244503] HE~
    但是[]内的是毫秒  现在想把[]内的数字替换成 标准的lrc格式的 分:秒:毫秒例如 第一句话 替换后就是
    [00:02:02] Le ciel obscure  
    我写的数字转为分:秒:毫秒String timeconvert(String str) { Date date = new Date(Integer.parseInt(str));
    SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
    return sf.format(date);
    }那位高手 能给点建议 有好的方法也可以说下
      

  6.   

    Scanner input = new Scanner(System.in);
    String str = input.next();
    Pattern reg = Pattern.compile("\\d+");
    Matcher matcher = reg.matcher(str);
    while (matcher.find())
        System.out.println(matcher.group());
      

  7.   


    public class PatternTest {
    public static void main(String[] args) throws IOException{
    String str = "[1234] hello";
    for(String tmp:str.split("\\D+")){
    if(tmp.length()>0){
    System.out.println(tmp);
    }
    }
    }
    }只提取数字出来
      

  8.   

    这样够详细了吧?public class PatternTest {
    public static void main(String[] args) throws IOException{
                    //歌词
    String[] lyrics = {"[2372] Le ciel obscure",
    "[2949] La solitude qui nous rend la peine",
    "[5603] La ceour brise"};
    ArrayList<String> numList = new ArrayList<String>();
                    //对每个歌词中的时间进行拆分
    for(String str:lyrics){
    numList.add(splitToNum(str));
    }
                    //输出拆分后的时间
    for(int i=0; i<numList.size(); i++){
    System.out.println("第" + i + "个时间:"+timeconvert(numList.get(i)));
    }
    }

    public static String splitToNum(String str){
    for(String tmp:str.split("\\D+")){
    if(tmp.length()>0){
    return tmp;
    }
    }
    return null;
    }

    public static String timeconvert(String str) {                Date date = new Date(Integer.parseInt(str));
                    SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
                    return sf.format(date);
        }
    }
      

  9.   

    可行,但还是太复杂
    看这个http://topic.csdn.net/u/20110417/13/107bbf03-59ac-485d-addd-62eefe2e804a.html
      

  10.   


        public static void main(String[] args)
        {
            String s = "[2372] Le ciel obscure\r\n[56789] world";
            Pattern p = Pattern.compile("\\[\\d+?\\]");
            Matcher m = p.matcher(s);
            while (m.find())
            {
                s = m.replaceFirst(toTime(m.group()));
                m = p.matcher(s);
            }
            System.out.println(s);    }    private static String toTime(String num)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
            long time = Long.parseLong(num.substring(1, num.length() - 1));
            return "[" + sdf.format(time) + ":" + ((time % 1000) / 10) + "]";
        }
      

  11.   

    我之前还以为你只是提取出数字就完了~原来你是想直接替换掉“[]”中的时间~那就更简单了public class PatternTest {
    public static void main(String[] args) throws IOException{
    String[] lyrics = "[2372] Le ciel obscure";

    String newStr = lyrics.replaceFirst("\\[\\d+\\]","[" + timeconvert(splitToNum(lyrics)) + "]");
    System.out.println(newStr);
    }

    public static String splitToNum(String str){
    for(String tmp:str.split("\\D+")){
    if(tmp.length()>0){
    return tmp;
    }
    }
    return null;
    }

    public static String timeconvert(String str) {        Date date = new Date(Integer.parseInt(str));
            SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
            return sf.format(date);
        }
    }
      

  12.   

    有个地方写错了~
    String[] lyrics = "[2372] Le ciel obscure";
    应该是String lyrics = "[2372] Le ciel obscure";
      

  13.   

    呵呵。。用18楼的方法 ,软件已完成
    我原来用s = m.replaceALL(toTime(m.group()));
    不行 没想到换成s = m.replaceFirst(toTime(m.group()));
    就行了,,,o(︶︿︶)o 唉
      

  14.   

    纠结了几天,,竟然换一个单词就行了悲剧啊
    谢谢  kyox0美女  你真热情呵感动
      

  15.   

    18楼的方法还有可以改进的地方。
    直接用表达式://[(//d+)//],然后用group(1),就没必要再substring了。
    还有,在while里用StringBuilder比较好。
      

  16.   

    public static void main(String[] args)
    {
        String s = "[2372] Le ciel obscure\r\n[56789] world";
        Pattern p = Pattern.compile("\\[(\\d+?)\\]");
        Matcher m = p.matcher(s);
        StringBuffer sb = new StringBuffer();
        while (m.find())
        {
            m.appendReplacement(sb, toTime(m.group(1)));
        }
        m.appendTail(sb);
        System.out.println(sb); } private static String toTime(String num)
    {
        long time = Long.parseLong(num);
        SimpleDateFormat sdt = new SimpleDateFormat("mm:ss");
        return "[" + sdt.format(time) + ":" + ((time % 1000) / 10) + "]";
    }
      

  17.   

    我java才学两个月,很多东西的用法都得现查doc。
    StringBuffer是个synchronized的methoed,性能不佳,但是mather里的appendReplacement只支持StringBuffer。
    因为要优先考虑StringBuilder,所以appendReplacement就不能用了,只能考虑用StringBuilder里的method了。
    现学现卖一下吧,下面是用StringBuilder的版本,在main里:            String s = "[2372] Le ciel obscure\r\n[56789] world";
        StringBuilder sb = new StringBuilder(s);
        Pattern p = Pattern.compile("\\[(\\d+)\\]");
        Matcher m = p.matcher(sb);
        while (m.find())
        {
         sb.replace(m.start(), m.end(), toTime(m.group(1)));
        }
        System.out.println(sb);