2013-02-13 38 views
0

我有一個字符串,我想從中刪除字符串Input! + word + digitsCalc! + word + digits。我也包括了我的嘗試。正則表達式:移除單詞和數字

 
Input : IF(Input!B34 + Calc!B45) 
 
Output : Input!B34 Calc!B45 

我嘗試:

 
Pattern findMyPattern = Pattern.compile("Input!\\w\\d|" + worksheetName+ "!.+?"); 
     Matcher foundAMatch = findMyPattern.matcher(input); 
     HashSet hashSet = new HashSet(); 
     while (foundAMatch.find()) { 
      String s = foundAMatch.group(0); 
      hashSet.add(s); 
     } 

我應該使用什麼正則表達式?我嘗試了使用其中的一些。但我不是他們的專家。一些想法會很有用。

+0

你的例子輸出有'Calc!45',但輸入有一個'B'字符 - 是不是應該保存? – voithos 2013-02-13 19:36:10

+0

@戰士。請記住將答案標記爲已接受。 – 2013-02-13 20:03:37

+0

完成! ........ – AnujKu 2013-02-13 21:04:19

回答

3

你可以使用這個表達式:

"(?:Input|Calc)![a-zA-Z]\\d+" 

說明:

(?:Input|Calc) // Match `Input or Calc` 
!    // Followed by ! 
[a-zA-Z]  // Followed by an alphabetical character 
\\d+   // Then digits. 

而且隨着Matcher#find使用它,然後添加matcher.group()Set

+1

@Downvoter ..請發表評論。由於我已經在OP輸入上測試了這些代碼,我真的很想聽聽你的一些信息。 – 2013-02-13 19:40:57