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

时间:2010-09-11 14:07:37

标签: java string

由于我不熟悉java,我不知道某个地方是否有可以做这件事的库。如果没有,是否有人有任何想法如何实现?

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

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

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

如果我有一个字符串“ac”,我需要的结果是: “AC” “公元前” “广告” “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

3 个答案:

答案 0 :(得分:1)

以下是使用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 :我创建了一个递归函数(实际上它是半递归的半迭代),它通过将第一个字符替换为指定集合中的字符来生成基于模板字符串的字符串: / p>

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]

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是:

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”。如果可以进一步优化,欢迎任何其他帮助。

相关问题