使用正则表达式

时间:2016-11-22 14:22:10

标签: java regex

我有一个HashMap,里面有键和值。我想用字符串中的地图值替换键。

在字符串中,键被写为 @keyName @" keyName" 这些应该替换为 map.get(" keyName")

让我们说我们的地图就是这个

"key1" : "2"  
"key2" : "3"  
"key3" : "4"  
"key 4" : "5"
"key-5" : "6"

因此,如果我们处理字符串" hello world,我已经@ key1岁了。" ,它将成为" hello world,我是2岁。"

我们可以使用 @" key1" ,而不是 @ key1 。如果我们在没有引号的情况下使用它,则空格(空格字符)或EOF应该跟在键名后面,并且键名不应该包含空格。但是如果键名中有一个空格,那么它应该是引号。

如果我们处理字符串" hello world,我就是@" key @" key1""在第一步它应该替换特殊字符串中的特殊字符串并成为" hello world,我是@" key2"已经过了。#34; 。岁了。" 然后第二步它应该是"你好世界,我3岁。"

我已经为一个特殊字符串完成了它,它不识别特殊字符串中的特殊字符串。这是代码:

private static Pattern specialStringPattern = Pattern.compile("@\"([^\"]*?)\"|@\\S+");

/** this replaces the keys inside a string with their values.
 * for example @keyName or @"keyName" is replaced with the value of the keyName. */
public static String specialStrings(String s) {
    Matcher matcher = specialStringPattern.matcher(s);
    while (matcher.find()) {
        String text = matcher.group();
        text = text.replace("@","").replaceAll("\"","");
        s = s.replace(matcher.group(),map.get(text));
    }
    return s;
}

抱歉我的英语,以及我缺乏正则表达式的知识。我认为通过稍微修改代码应该很容易得到答案。

以下是我需要的一些例子:

There is @key1 apples on the table.  
There is 2 apples on the table.

There is @"key1" apples on the table.  
There is 2 apples on the table.

There is @key 4 apples on the table.
There is null 4 apples on the table.

There is @"key 4" apples on the table.
There is 5 apples on the table.

There is @key@key2 apples on the table.
There is @key3 apples on the table. (second step)
There is 4 apples on the table. (final output)

There is @"key @"key3"" apples on the table.
There is @"key 4" apples on the table. (second step)
There is 5 apples on the table. (final output)

There is @"key @key3" apples on the table.
There is @"key 4" apples on the table. (second step)
There is 5 apples on the table. (final output)

There is @"key @key3 " apples on the table.
There is @"key 4 " apples on the table. (second step)
There is null apples on the table. (final output)

There is @key-5 apples on the table.
There is 6 apples on the table.

2 个答案:

答案 0 :(得分:1)

我制作的正则表达式符合您的示例: https://regex101.com/r/nudYEl/2

@(\"[\w\s]+\")|(?!@(\w+)@(\w+))@(\w+)

你需要修改你的函数来递归:

public static String specialStrings(String s) {
    Matcher matcher = specialStringPattern.matcher(s);
    boolean findAgain = false;
    while (matcher.find()) {
        String text = matcher.group();
        text = text.replace("@","").replaceAll("\"","");
        s = s.replace(matcher.group(),map.get(text));
        findAgain = true;
    }
    if (findAgain) return specialStrings(s);
    return s;
}

[更新]
正则表达式:https://regex101.com/r/nudYEl/4

@(\"[\w\s-]+\")|(?!@([\w-]+)@([\w-]+))@([\w-]+)

答案 1 :(得分:0)

不要使用正则表达式:

for (boolean hit = true; hit;) {
    hit = false;
    for (String key : map.keySet()) {
        if (str.contains("@\"" + key + "\"")) {
            str = str.replace("@\"" + key + "\"", map.get(key));
            hit = true;
        } else if (str.contains("@" + key  )) {
            str = str.replace("@" + key + "", map.get(key));
            hit = true;
        }
    }
}