2013-05-01 15 views
1

我試圖解決這個問題近3天。我仍然不知道如何解決它。
有一個輸入字符串(例如):嘗試使用正則表達式解析和替換Java中的模式

In software, a stack overflow [apple] occurs when too much memory [orange] is used on the call stack [banana]. 
The call stack [pear] contains a limited amount of memory, often determined at the start of the program [apple]. 

我喜歡做的是替換單詞[apple][orange][banana][pear]喜歡的東西<img src="apple.jpg"><img src="orange.jpg"><img src="banana.jpg"><img src="pear.jpg">
事實上,經過近1天,我發現了一個正則表達式,可以找出啓動模式與"[""]",這是(?<=\\[)\\w+(?=])
我不知道如何存儲單詞列表([蘋果]結束, [橙子]...)。
我應該使用HashMap還是ArrayList
以及如何循環瀏覽HashMapArrayList以替換爲'最快時間'中的相應字符串?

在這個例子中,列表只包含4件事情。但事實上,列表中可能有500多件事。
儘管我發現了這個模式,但我仍然無法解決這個問題,因爲我不知道如何在輸入字符串中找到所有模式,然後找出所有模式,然後檢查列表中的這個模式,以及然後用正確的字符串替換。
請注意,在此示例中,[apple]被替換爲<img src="apple.jpg">,但實際上[xxx]中的xxx.jpg可能不相同。但我有這個映射的列表。
我真的想解決這個問題,請幫我解決並提供樣品編碼。
非常感謝。

+0

使用'HashMap '。 – sircapsalot 2013-05-01 14:15:20

+0

其實,這是我第一次使用正則表達式,我真的不知道如何實現它,你能提供一些例子嗎? – jjLin 2013-05-01 14:16:58

+0

如果您堅持使用RegEx,我將會提出使用RegEx的解決方案的另一個答案。甚至可能提供時間示例 – sircapsalot 2013-05-01 14:45:38

回答

1
String poem = "In software, a stack overflow [apple] occurs" 
    + " when too much memory [orange] is used on the call stack [banana]." 
    + " The call stack [pear] contains a limited amount of memory," 
    + " often determined at the start of the program [apple]."; 

Map<String, String> rep = new HashMap<String, String>(); 

rep.put("[apple]", "<img src='apple.jpg' />"); 
rep.put("[banana]", "<img src='banana.jpg' />"); 
rep.put("[orange]", "<img src='orange.jpg' />"); 
rep.put("[pear]", "<img src='pear.jpg' />"); 

for (Map.Entry<String, String> entry : rep.entrySet()) { 
    poem = poem.replace(entry.getKey(), entry.getValue()); 
} 

// poem now = what you want. 
+0

您的意思是'詩=詩。替換(水果,rep.get(水果));'。 – Keppil 2013-05-01 14:20:02

+0

抓住了它,因爲我說。謝謝:) – sircapsalot 2013-05-01 14:20:22

+0

好的解決方案,在這些簡單的情況下不需要使用正則表達式。 +1。 – Keppil 2013-05-01 14:20:49

0

如果你堅持使用正則表達式完成這個任務......

String poem = "In software, a stack overflow [apple] occurs" 
       + " when too much memory [orange] is used on the call stack [banana]." 
       + " The call stack [pear] contains a limited amount of memory," 
       + " often determined at the start of the program [apple]."; 

     List<String> fruits = new ArrayList<String>(); 
     fruits.add("[apple]"); 
     fruits.add("[banana]"); 
     fruits.add("[pear]"); 
     fruits.add("[orange]"); 

     String pattern = "\\[(?<=\\[)(\\w+)(?=])\\]"; 
     poem = poem.replaceAll(pattern, "<img src='$1.jpg' />"); 

     System.out.println(poem); 

你可以看到代碼的this dynamic run

+0

對不起,什麼意思是「$ 1.jpg」($ 1)。如何正確替換正確的字符串? – jjLin 2013-05-02 08:13:21

0

我還是新來的正則表達式,但我相信你想要做的是使用分組和模式和匹配器來取代比賽的特定部分。

你想分組你的正則表達式,並用相關的代碼只替換「[」和「]」。

String poem = "In software, a stack overflow [apple] occurs when too much memory [orange] is used on the call stack [banana]. The call stack [pear] contains a limited amount of memory, often determined at the start of the program [apple]."; 
Pattern p = Pattern.compile("(\\[)(\\w*)(\\])"); 
Matcher m = p.matcher(poem); 
poem = m.replaceAll("<img src='$2.jpg' />"); 

這是我爲了讓它在你的例子上工作。希望這有助於(至少幫助我學習正則表達式!)。

+0

抱歉,「$ 2.jpg」($ 2) – jjLin 2013-05-02 06:36:45