2017-10-21 68 views
-1

我有寫在這樣各行的名稱的文件創建如果我運行正則表達式搜索[az] [az],它會返回「su bh as ch an yo ...」。 是否有regexp模式可以返回這種形式的匹配「」su ub ha as sh ch ha andnd ...「?這個正則表達式的工作方式就像長度爲'2'的標記符。如果regexp是一個有效的Java 。正則表達式標記生成器長度爲n的使用正則表達式

回答

1

試試這個正則表達式:

(?=([a-zA-Z]{2})) 

這會向前看的字符串,如果字符串它匹配[a-zA-Z]{2}後匹配一個空字符串,然後它把2個字符後,成團以來。引擎會檢查每一個索引,這會返回你的預期結果。

你只需要得到所有的g比賽

final String regex = "(?=([a-zA-Z]{2}))"; 
final String string = "subhash chand\n" 
     + "yobie chimwanachomama\n" 
     + "riadh chaieb"; 

final Pattern pattern = Pattern.compile(regex); 
final Matcher matcher = pattern.matcher(string); 

while (matcher.find()) { 
    System.out.println(matcher.group(1)); 

} 

Try it here!

+0

你好roup1s,它返回輸出「蘇BH爲CH的喲雙通道IMはなCH OM上午裏的廣告CH人工智能EB」,所需的輸出爲「蘇ub bh ha sh sh ch ha and an ...「 –

+0

爲什麼它不匹配'bh'? @AshishJain – Sweeper

+0

是的,請原諒錯誤,結果是每個連續的兩個字符長度標記:「su ub bh ha as sh ch ha ha andnd」 –