繁体   English   中英

用正则表达式组合替换子字符串

[英]Replace substring with a regex combination

由于我对Java不太熟悉,所以我不知道某个地方是否有可以执行此操作的库。 如果没有,那么有人有什么想法可以实现吗?

例如,我有一个字符串“ foo”,我想将字母f更改为“ f”和“ a”,以便该函数返回具有值“ foo”和“ aoo”的字符串列表。

当有更多相同的字母时该如何处理? “ ffoo”分为“ ffoo”,“ afoo”,“ faoo”,“ aaoo”。

更好的解释:(((“ a”,(“ a”,“ b)),(” c“,(” c“,” d“))))上面是一组字符,需要用一个字符替换在另一个元素中,“ a”将替换为“ a”和“ b”,“ c”将替换为“ c”和“ d”。

如果我有字符串“ ac”,则需要的结果组合为:“ ac”“ bc”“ ad”“ bd”

如果字符串是“ IaJaKc”,则结果组合为:“ IaJaKc”,“ IbJaKc”,“ IaJbKc”,“ IbJbKc”,“ IaJaKd”,“ IbJaKd”,“ IaJbKd”,“ IbJbKd”

组合的数量可以这样计算:(replacements_of_a ^ letter_amount_a)*(replacements_of_c ^ letter_amount_c)第一种情况:2 ^ 1 * 2 ^ 1 = 4第二种情况:2 ^ 2 * 2 ^ 1 = 8

例如,如果组是((“ a”,(“ a”,“ b)),(” c“,(” c“,” d“,” e“)))),并且字符串是“ aac ”,组合数为:2 ^ 2 * 3 ^ 1 = 12

这是您的foo和aoo示例代码

public List<String> doSmthTricky (String str) {
    return Arrays.asList("foo".replaceAll("(^.)(.*)", "$1$2 a$2").split(" "));
}

对于输入“ foo”,此方法返回包含2个字符串“ foo”和“ aoo”的列表。

仅当输入字符串中没有空格(示例中为“ foo”)时,它才有效。 否则它会更复杂。

当有更多相同的字母时该如何处理? “ ffoo”分为“ ffoo”,“ afoo”,“ faoo”,“ aaoo”。

我怀疑正则表达式是否可以帮助您,您想基于初始字符串生成字符串,这不是regexp的任务。

UPD :我创建了一个递归函数(实际上是半递归半迭代),该函数通过将模板的第一个字符替换为指定集合中的字符来基于模板字符串生成字符串:

public static List<String> generatePermutations (String template, String chars, int depth, List<String> result) {
    if (depth <= 0) {
        result.add (template);
        return result;
    }
    for (int i = 0; i < chars.length(); i++) {
        String newTemplate = template.substring(0, depth - 1) + chars.charAt(i) + template.substring(depth);
        generatePermutations(newTemplate, chars, depth - 1, result);
    }
    generatePermutations(template, chars, depth - 1, result);
    return result;
}

参数@depth表示应从字符串开头替换多少个字符。 排列数(chars.size() + 1) ^ depth

测试:

System.out.println(generatePermutations("ffoo", "a", 2, new LinkedList<String>()));

Output: [aaoo, faoo, afoo, ffoo]

--
System.out.println(generatePermutations("ffoo", "ab", 3, new LinkedList<String>()));

Output: [aaao, baao, faao, abao, bbao, fbao, afao, bfao, ffao, aabo, babo, fabo, abbo, bbbo, fbbo, afbo, bfbo, ffbo, aaoo, baoo, faoo, aboo, bboo, fboo, afoo, bfoo, ffoo]

我不确定您需要什么。 请指定来源和期望的结果。 无论如何,您应该为此使用标准的Java类:java.util.regex.Pattern,java.util.regex.Matcher。 如果您需要处理开头的重复字母,则有两种方法,使用符号“ ^”-表示行的开头,或者出于相同的目的,您可以使用“ \\ w”快捷方式,即开头的这个单词。 在更复杂的情况下,请查看“向后看”表达式。 您可以在java doc中的java.util.regex中找到这些技术的完整描述,如果还不够的话,请访问www.regular-expressions.info。

这里是:

public static void returnVariants(String input){
        List<String> output = new ArrayList<String>();
        StringBuffer word = new StringBuffer(input);
        output.add(input);

        String letters = "ac";
        int lettersLength = letters.length();
        int wordLength = word.length();
        String replacement = "";

        for (int i = 0; i < lettersLength; i++) {
            for (int j = 0; j < wordLength; j++) {
                if(word.charAt(j)==letters.charAt(i)){
                    if (word.charAt(j)=='a'){
                        replacement = "ab";
                    }else if (word.charAt(j)=='c'){
                        replacement = "cd";
                    }
                    List<String> tempList = new ArrayList<String>();
                    for (int k = 0; k < replacement.length(); k++) {
                        for (String variant : output){
                            StringBuffer tempBuffer = new StringBuffer(variant);
                            String combination = tempBuffer.replace(j, j+1, replacement.substring(k, k+1)).toString();
                            tempList.add(combination);
                        }
                    }
                    output.addAll(tempList);
                    if (j==0){
                        output.remove(0);
                    }
                }
            }
        }
        Set<String> uniqueCombinations = new HashSet(output);
        System.out.println(uniqueCombinations);
    }

如果输入为“ ac”,则返回的组合为“ ac”,“ bc”,“ ad”,“ bd”。 如果可以进一步优化,欢迎您提供任何其他帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM