String aa  = "111 222 333 444 123 1234";
现在匹配 以1开头空格结尾的字符,结果是:111 123
String regEx1 = "(1.*? )";
Pattern pattern1 = Pattern.compile(regEx1);
Matcher matcher1 = pattern1.matcher(aa);
while(matcher1.find()){
System.out.println(matcher1.group());
}
我想匹配 不是1开头空格结尾的字符,结果是:222 333 444 1234
这组正则该怎么写?谢谢大家,在线等。java正则

解决方案 »

  1.   


    这个不用正则可能更简单吧,string.charAt(0)!=1和string.charAt()!=''
      

  2.   


    这个不用正则可能更简单吧
    string.charAt(0)!=1和string.charAt(string.length()-1)!=''就行了
      

  3.   

    string.charAt(0) 主要要强转int型
      

  4.   

    \\b[02-9][0-9]*\\b
    试试这个
    \\b代表词边界
    [02-9]除1以外所有的数字打头
    后面[0-9]随意
      

  5.   

    感谢楼上的几位,换个字符串 String aa = "aaa bbb  abab ccc accc";  匹配a开头的字符串,并且结尾是空格的,结果有:aaa abab 
      

  6.   


    public static void main(String[] args) throws Exception {
    String aa  = "111 222 333 444 123 1234";
    Matcher m = Pattern.compile("1[^\\s]*?(\\s|$)").matcher(aa);
    //现在匹配 以1开头空格结尾的字符,结果是:111 123
    while(m.find()){
    System.out.println(m.group());
    }
    }