Java正则表达式拆分文本(分隔符和顺序可能都是未知的)

时间:2014-06-10 06:09:05

标签: java regex

我试图分割像"name:jack,berk,john;teacher:smith,jan;course:math;"这样的文字。我希望结果包含3个子字符串(或更少,取决于' name''老师'当然'),这是:

  • "name:jack,berk,john;"
  • "teacher:smith,jan;"
  • "course:math;"

但是标识符'teacher,name,course'的外观顺序并不固定,它可以是'course ,name, teacher',也可以缺少一个或两个,就像只有名称'标识符

标识符之间的分隔符也不固定,在示例中为';',但也可以是'、\\s,'

我已多次尝试,但不起作用。

String str = "name:jack,berk,john;teacher:smith,jan;course:math;
str = str.replaceAll("(.*)(.)(name|teacher|course)(.*)(.)(name|teacher|course)(.*)", "$1--$3$4--$6$7");
System.out.println(str);

任何建议都将不胜感激。

4 个答案:

答案 0 :(得分:4)

编辑:正则表达式,无需查找特定的分隔符。

不是拆分字符串而是在这个正则表达式上匹配:

(name|teacher|course):(.+?)(?=\W*(?:name|teacher|course|$))

Working Demo

<强>代码:

Pattern p = Pattern.compile("(name|teacher|course):(.+?)(?=\\W*(?:name|teacher|course|$))");
Matcher m = p.matcher(name:jack,berk,john;teacher:smith,jan;course:math;);
while (m.find()) {
   System.out.println(m.group(1) + " :: " + m.group[2]);
}

答案 1 :(得分:1)

public static void main(String[] args) {
    String str = "name:jack,berk,john;teacher:smith,jan;course:math;";
    String[] values = str.split(";");

    for (String s : values) {
        if (s.contains("name:")) {
            System.out.println("name : " + s.replaceAll("name:", ""));
        } else if (s.contains("teacher:")) {
            System.out.println("teacher : " + s.replaceAll("teacher:", ""));
        } else if (s.contains("course:")) {
            System.out.println("course : " + s.replaceAll("course:", ""));
        }
    }
}

O / P:

name : jack,berk,john
teacher : smith,jan
course : math

答案 2 :(得分:0)

您可以使用唯一分隔符替换所有分隔符,然后使用String.split

String input = "name:jack,berk,john;teacher:smith,jan-course:math;";
String uniqueDelimiter = ";";
String[] otherDelimiters = new String[2];
otherDelimiters[0] = "\\s";
otherDelimiters[1] = "-";
for (String delimiter : otherDelimiters) {
    input = input.replaceAll(delimiter, uniqueDelimiter);
}
String[] keyList = input.split(uniqueDelimiter);

答案 3 :(得分:0)

str.split("[、;,\\s](?=name|teacher|course|$)")