修改动态输入字符串

时间:2018-02-26 14:46:29

标签: java regex

我正在处理一项要求。我收到以下格式的输入字符串。

A:82% X 18% Y B:100% X C:82% X 18% Y AB:60% X 20% Y 20% ZZ

字符串说明,

1) String consists of multiple material names like below are the material names present in above mentioned string

        A
        B
        C
        AB

2) Every material is made up of different constituents, For example A is made from 82% of X and 18% of Y. In another input string according to material name the ratio of ingredients can split accordingly. But total is always 100%

3) A string can have multiple material names and one material can be made of n number of ingredients (Total percentage would be 100%)

我想以下面的格式转换我的输入字符串

#A:82% X,18% Y #B:100% X #C:82% X, 18% Y #AB:60% X,20% Y,20% ZZ

我能够使用正则表达式实现哈希部分,代码片段

String inp = "A:82% X 18% Y B:100% X C:82% X 18% Y AB:82% X 18% Y";
String regex = "(\\b[A-Za-z]{1,}\\:\\b)";   
System.out.println(inp.replaceAll(regex, "#$1"));

但我无法处理或不知道在特定材料的成分之间设置逗号。

请提出任何建议吗?

2 个答案:

答案 0 :(得分:1)

这是一个利用Java 8流和正则表达式的可能解决方案。

String input = "A:82% X 18% Y B:100% X C:82% X 18% Y AB:60% X 20% Y 20% ZZ";
System.out.println(
        // streaming chunks of input delimited by start of expression
        Stream.of(
            input.split("(?=(^| )\\p{L}+:)")
        )
        // mapping each chunk to replacements
        .map(
            s ->
                // pre-pending # 
                s.replaceAll("(\\p{L}+:)", "#$1")
                // pre-pending comma for multiple value percentages
                .replaceAll("(?= \\d+% \\p{L})",",")
        )
        // collecting by trivial join
        .collect(Collectors.joining())
);

<强>输出

#A:82% X, 18% Y #B:100% X #C:82% X, 18% Y #AB:60% X, 20% Y, 20% ZZ

答案 1 :(得分:0)

您可以使用否定前瞻:

String inp = "A:82% X 18% Y B:100% X C:82% X 18% Y AB:82% X 18% Y";
String regex = "(\\b[A-Za-z]{1,}\\:\\b)";
String regexToPlaceComa = "(\\d{1,3}% [a-zA-Z]+) (?!#)";
inp = inp.replaceAll(regex, "#$1");
inp = inp.replaceAll(regexToPlaceComa, "$1,");
System.out.println(inp);

同样在你的正则表达式中:“\\”字符转义在“:”之前是多余的,而“{1,}”你可以用“+”替换如下:

regex = "(\\b[A-Za-z]+:\\b)";