2017-02-16 69 views
2

我試圖創建一個正則表達式,讓我從我感興趣的一個輸入幾個組合對。正則表達式來產生一個單詞以連續字母

如果我有以下的輸入...

「pppeeeople」

我想創建以下排列...

人,ppeeople,peeople,ppeople。

我目前可以用以下兩個REGEX創建'people'和'ppeeople',其中輸入詞是'pppeeeople'。

String temp = word.replaceAll("(.)\\1{2,}", "$1$1"); //result ppeeople 
String temp2 = word.replaceAll("(.)\\1{1,}", "$1"); //result people 

但是我想能夠產生一個正則表達式也可以用1套兩個連續程序字母搶詞,如「ppeople」,然後另一個正則表達式,可以跳過第一inputed雙字符序列併產生'peeople'等等。基本的想法是獲得包含兩個連續字符的單詞的所有排列。

這是可能與REGEX或我應該只是使用循環和StringBuilder到列表中?

這很接近,但它會產生超出範圍的索引錯誤,我仍然必須從ArrayList重複項中刪除重複項,並將其添加到。

int index = temp.length(); 

     for (int i = 0; i < index; i++) { 
      System.out.println("Index" + i + ": " + temp); 
      if(temp.charAt(i) == temp.charAt(i+1)) { 
       StringBuilder sb = new StringBuilder(temp); 
       repeats.add(temp); 
       sb.deleteCharAt(i); 
       temp = sb.toString(); 
      } 
     } 
+2

「我應該只是使用循環和StringBuilder到列表中?」幾乎總是,是的。 – Sneftel

+1

更好的方法是構建帶回溯遞歸的字符串。如果你需要這樣的事情,我可以爲你寫一個片段。 –

+0

我這樣做 - 但會遞歸比只使用StringBuilder或子字符串方法更好嗎?我有一個不可思議的子字符串循環,幾乎做我想要的,但還不完全。 – Yawn

回答

0

對不起,沒有單個正則表達式會返回所有的排列字符串。相反,我更喜歡使用遞歸方法,它將返回列表中的所有字符串。

如果您想這樣做,請參閱answers here

在C++中,stl算法中有一個名爲next_permutation()的函數,它返回給定字符串中的下一個置換字符串。

UPDATE

據這裏的問題的編輯是一個片段來獲得給定的與字符串

public class Combination { 

    //Use Set/HashSet instead of ArrayList if you do not want duplicate string 
    //Set<String> combinations = new HashSet<String>(); 
    private ArrayList<String> combinations = new ArrayList<>(); 

    public void generate(String instr) { 
     generate(instr, new StringBuffer(), 0); 
    } 
    private void generate(String instr, StringBuffer outstr, int index) { 
     for (int i = index; i < instr.length(); i++) { 
      outstr.append(instr.charAt(i)); 

      // Here you may add your rules to avoid all combinations 
      combinations.add(outstr.toString()); 

      generate(instr, outstr, i + 1); 
      outstr.deleteCharAt(outstr.length() - 1); 
     } 
    } 

    public ArrayList<String> getCombinations() { 
     return combinations; 
    } 
} 

這裏combinations數組列表將包含所有的組合的組合如預期。你現在可以把它像這個 -

Combination cmb = new Combination(); 

cmb.generate("pppeeeople"); 

ArrayList<String> list = cmb.getCombinations(); 

for(String str : list){ 
    System.out.println(str); 
} 

如果你想少組合,而不是全部,你應該根據長度,字符,重複等添加自己的規則

+0

不尋找所有的排列組合。我正在尋找一些重複的組合。如果你有'hheelloo',我想要返回hhelo,heelo,hello,heloo。 – Yawn

+0

但是在你提到的關於排列的問題的第一行中。好的,我會更新它。現在讓我知道,你是否也想從左到右保持字母順序? –

+0

是的,我相信 – Yawn

0

不漂亮,但它確實產生期望的輸出。

int index = temp.length(); 

    for (int i = 0; i < index; i++) { 
     // System.out.println("Index" + i + ": " + temp); 
     try{ 
      if(temp.charAt(i) == temp.charAt(i+1)) { 
       StringBuilder sb = new StringBuilder(temp); 
       repeats.add(temp); 
       sb.deleteCharAt(i); 
      // System.out.println("Sb after delete: " + sb.toString()); 
       temp = sb.toString(); 
      // System.out.println("New Temp: " + temp); 
      } 
     } catch (Exception e) { 
      //print.stackTrace(e); 
     } 
    } 
相關問題