简体   繁体   English

尝试使用正则表达式解析和替换Java模式

[英]Trying to parse and replace pattern in Java using regex

I have tried to solve this problem for nearly 3 days. 我已经尝试解决了近3天的问题。 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"> . 我想做的是将[apple][orange][banana][pear]一词替换为<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+(?=]) 实际上,经过将近1天的时间,我发现了一个正则表达式,可以找到以"["开头和以"]"结尾的模式,即(?<=\\\\[)\\\\w+(?=])
I don't know how to store a list of words([apple],[orange]...). 我不知道如何存储单词列表([apple],[orange] ...)。
Should I use HashMap or an ArrayList ?? 我应该使用HashMap还是ArrayList
And how to loop through the HashMap and ArrayList to replace to corresponding string in the 'fastest time'? 以及如何在HashMapArrayList循环以在“最快时间内”替换为对应的字符串?

In this example, the list only contain 4 things. 在此示例中,列表仅包含4个内容。 But in fact, it may be more than 500 things in the list. 但实际上,列表中可能有500多个东西。
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 ]. 请注意,在此示例中, [apple]被替换为<img src="apple.jpg"> ,但实际上[ xxx ]中的xxx .jpg可能不同。 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. 我对regex还是很陌生,但是我相信您想要做的是使用分组以及模式和匹配器来替换匹配的特定部分。

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!). 希望能有所帮助(它至少帮助我学习了正则表达式!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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