根据java中的典型条件拆分字符串?

时间:2017-01-24 15:25:48

标签: java regex

我的输入字符串如下:

String sample1 = "productName/@languageCode";
String sample1="brandNameInformation/languageSpecificBrandName/@languageCode";

我想只在出现" /"时才拆分它。而不是" / @"

我该怎么办? 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以使用negative lookahead

import java.util.Arrays;

public class Main16 {
    public static void main(String[] args) {
        String sample1 = "productName/@languageCode";
        String sample2 ="brandNameInformation/languageSpecificBrandName/@languageCode";

        String regex = "/(?!@)";

        System.out.println(Arrays.deepToString(sample1.split(regex)));
        System.out.println(Arrays.deepToString(sample2.split(regex)));
    }
}

输出:

[productName/@languageCode]
[brandNameInformation, languageSpecificBrandName/@languageCode]