简体   繁体   中英

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.

If you are stuck on using Regular Expressions for this task...

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);

You can see this dynamic run of the code.

I'm still new to regex but I believe what you want to do is use grouping and a pattern and matcher to replace specific parts of the match.

You want to group your regex and replace only the "[" and "]" with the related code.

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' />");

This is what I did to get it to work on your example. Hope that helps(it helped me learn regex a little more at least!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM