String replaceAll-正则匹配-截取以指定字符开头,以指定字符结尾的字符串

scala代码块

截取以某个字符开头,以某个字符结尾的字符串

def main(args: Array[String]): Unit = {
        val s = "{{a61,a2,a3},{b1,b2,b3},{c1m,.,kkl,c2,c3}}"
        val reg = Pattern.compile("\\{(\\w+?),")
        val matcher = reg.matcher(s)
        while (matcher.find()) {
            println(matcher.group(1))
        }
    }

运行结果

a61
b1
c1m

java代码块


    public static void main(String[] args) {
        test t = new test();
        String a = "a+-b+ -c+ -d -e";
        System.out.println(a);
        System.out.println(a.replaceAll("(-|\\+| )",","));
    }

运行结果

a±b+ -c+ -d -e
a,b,c,d,e

你可能感兴趣的:(java)