现有如下格式的字符串:
today is '#fine'   #first line #fine
you are '#'welcome
规则:匹配每一行第一个不在引号之间的#之后的结果的字符串
匹配结果为:  #first line #fine

解决方案 »

  1.   

    手机上没法试。
    我觉得简单点做,就用正则把 在'里的#先replace掉。
    然后直接indexOf #来找。
      

  2.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class WangYingBin { public static void main(String[] args) {
    String str = "today is '#fine' #first line #fine";
    String regex = "(.*'#.*'.*?)(#.*)";
    Matcher mc = Pattern.compile(regex).matcher(str);
    while(mc.find()){
    System.out.println(mc.group(2));
    }
    }}
    运行结果:
    #first line #fine
      

  3.   


    你试试看这个测试用例?
    String str = "today is #first line #fine";
    什么输出都没了。
      

  4.   


    我的做法有问题,比如:
    today is '#fine' #first line #fine '#fine' 你希望的结果应该是
    #first line #fine '#fine'吧, 我做出来估计会变成。
    #first line #fine 'fine'  最后在引号里的#也会被去掉的。
      

  5.   


    嘿嘿,这样OK啦吧? 楼主也试试看。
    public static void main(String[] args) {
        String str = "today is '#fine' #first line #fine";
        System.out.println(getSharpComment(str));
        str = "today is #first line #fine";
        System.out.println(getSharpComment(str));
        str = "today is '#fine' #first line #fine '#fine'";        
        System.out.println(getSharpComment(str));
    }public static String getSharpComment(String str) {
    String regex = "('.*)(#)(.*')";
    String tempStr = str.replaceAll(regex, "$1@$3");
    int index = tempStr.indexOf("#");
    if (index != -1) {
    return str.substring(index, str.length());
    } else {
    return str;
    }
    }
      

  6.   

    String tempStr = str.replaceAll(regex, "$1@$3");
    上面这句代码的意思是?不懂,还麻烦请解释下
      

  7.   


    String regex = "('.*)(#)(.*')";
    String tempStr = str.replaceAll(regex, "$1@$3");就是把'里的#给去掉。
    $1是'里#前面的字符, $3是'里面#后面的字符。#9写的有bug。
    再给你一个。嘿嘿。
    public static void main(String[] args) {
        String str = "today is '#fine' #first line #fine";
        System.out.println(getSharpComment(str));
        str = "today is #first line #fine";
        System.out.println(getSharpComment(str));
        str = "today is '#fine' #first line #fine '#fine'";        
        System.out.println(getSharpComment(str));
        
        str = "today is fine";
        System.out.println(getSharpComment(str));
        
        //引号里有多个#的时候,#9写的时候有bug
        str = "today is '#fi#ne' #first line #fine '#fine'";
        System.out.println(getSharpComment(str));
    }public static String getSharpComment(String str) {
    String regex = "'[^']+'";
    Pattern p = Pattern.compile(regex);
    Matcher matcher = p.matcher(str);
    String tempStr = str;
    String group;
    while (matcher.find()) {
    group = matcher.group();
    tempStr = tempStr.replace(group, group.replace("#", "@"));
    }

    int index = tempStr.indexOf("#");
    if (index != -1) {
    return str.substring(index, str.length());
    } else {
    return "";
    }
    }
      

  8.   

    上面这段代码没问题。还有一个问题:
    tempStr = tempStr.replace(group, group.replace("#", "@"));
    这句话又是什么意思?麻烦解释下。
      

  9.   

    group为匹配类似'xxxxx'的子字符串。 x为任意字符。
    group.replace("#", "@")返回把#换成@
    tempStr.replace(group, group.replace("#", "@")); 就是把你的string里''里的#换成@ '@可以是#以外的任何字符。之后找了下,剩下的第一个#的位置。
    最后substring时还是用了原来的字符串。