簡體   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