Trying to parse and replace pattern in Java using regex

I have tried to solve this problem for nearly 3 days. And I still don't know how to solve it. There is an input string (for example):

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].

What I like to do is replace the word [apple], [orange], [banana], [pear] to something like <img src="apple.jpg"> , <img src="orange.jpg">, <img src="banana.jpg">, <img src="pear.jpg">. Actually, After nearly 1 day, I found out a regex that can find out the pattern starting with "[" and end with "]", which is (?<=\\[)\\w+(?=]) I don't know how to store a list of words([apple],[orange]...). Should I use HashMap or an ArrayList?? And how to loop through the HashMap and ArrayList to replace to corresponding string in the 'fastest time'?

In this example, the list only contain 4 things. But in fact, it may be more than 500 things in the list. Although I found out the pattern, I still can't solve this problem because I don't know how to find all the pattern in the input string and then find out all pattern and then check if this pattern in the list, and then replace with the correct string. Note that in this example, [apple] is replace with <img src="apple.jpg">, but in fact the xxx.jpg may not the same in [xxx]. But I have a list of this mapping. I really want to solve this problem, please help me to solve and provide sample coding. Thank you very much.


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.

Need Your Help

Monitoring GDI calls

windows monitoring gdi

Is there a tool that allows one to monitor GDI calls?

Entity Framework model binding/editor templates entity-framework entity-framework-6

What the best way to get all tags for article in comma separated textbox in ASP.NET MVC View, redact them, click save and add new/existing or remove tags from editied article in "Edit" Action? DB

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.