【问题标题】:How to replace contents of a regex如何替换正则表达式的内容
【发布时间】:2014-04-06 04:37:58
【问题描述】:

今晚我的大脑可能无法正常工作,但我需要一些帮助。

我有这个字符串

[9][1][1][ ][ ][ ][ ][ ][ ][9][a][b][ ][ ]

我想用不同的字符替换括号中的任何内容。 为简化起见,我们只说 x。

但它不会是 x。我将通过一个将每个数字更改为颜色代码的函数来运行它。

所以完成后它会看起来像这样。 [x][x][x][ ][ ][ ][ ][ ][ ][x][x][x][ ][ ]

我尝试了一些可笑的 string.replace() 函数,但无济于事。

编辑: 每个号码我都运行这个函数

public ChatColor getColor(String id)
{
    ChatColor color = ChatColor.WHITE;
    if(id == "0") color = ChatColor.BLACK;
    if(id == "1") color = ChatColor.DARK_BLUE;
    if(id == "2") color = ChatColor.DARK_GREEN;
    if(id == "3") color = ChatColor.DARK_AQUA;
    if(id == "4") color = ChatColor.DARK_RED;
    if(id == "5") color = ChatColor.DARK_PURPLE;
    if(id == "6") color = ChatColor.GOLD;
    if(id == "7") color = ChatColor.GRAY;
    if(id == "8") color = ChatColor.DARK_GRAY;
    if(id == "9") color = ChatColor.BLUE;
    if(id == "a") color = ChatColor.GREEN;
    if(id == "b") color = ChatColor.AQUA;
    if(id == "c") color = ChatColor.RED;
    if(id == "d") color = ChatColor.LIGHT_PURPLE;
    if(id == "e") color = ChatColor.YELLOW;
    if(id == "f") color = ChatColor.WHITE;

    return color;
}

【问题讨论】:

  • 不要使用==比较字符串。请改用.equals

标签: java regex string replace


【解决方案1】:

首先,为什么不提取字符串上的每个单独的字符,应用一些函数来更改字符以适合您指定的某些颜色代码,然后重新组合字符串后缀。您可以在一个循环中运行循环,并根据需要多次执行此操作(我的意思是,将底部的 for 循环放入另一个 for 循环中,具体取决于流程将要进行的迭代次数和各种条件)。例如,您有:

String s = [9][1][1][ ][ ][ ][ ][ ][ ][9][a][b][ ][ ]

接下来让我们循环遍历每个字符,并在每个字符 c 处根据某些条件替换字符。这里有多种替换选项,我只是将 cmets 留在了我可能会尝试的相关位置。这是建议的 for 循环。

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        

    //Process char

    //Add new character to a new string here and then reassemble the string

}

【讨论】:

  • 这肯定是一种方法。如果我能找到一个正则表达式示例,我可以使用它。这甚至可能比使用正则表达式更快。但话说回来,我也是来这里学习新事物的。
  • 太棒了。很高兴这有帮助。我会更多地考虑正则表达式的方法,看看我是否能想出一些有形的东西。目前,这种基本方法应该可以完成这项工作。祝你好运。
【解决方案2】:

这是一个使用前瞻的解决方案。

    String input = "[9][1][1][ ][ ][ ][ ][ ][ ][9][a][b][ ][ ]";
    // Map of color codes.
    HashMap<String,String> colorMap; 
    String output = input.replaceAll("(?<=\\[)\\S*?(?=\\])","x");

根据您对 devnull 答案的评论,听起来您实际上想要一个映射替换:

    String input = "[9][1][1][ ][ ][ ][ ][ ][ ][9][a][b][ ][ ]";
    // Map of color codes.

    for (int i = 0; i < 16; i++) {
        String current = Integer.toString(i, 16);
        input = input.replaceAll("(?<=\\[)" + current + "(?=\\])",getColor(current));
    }
    System.out.println(input);

请注意,此解决方案假定ChatColor 具有合理的toString 方法。

【讨论】:

  • 让我试试这个,看看会发生什么。谢谢你的时间!稍后我会在这里告诉你。
  • 我还要初始化什么颜色映射?
  • @Snhp9,我会根据你的更新更新我的答案。
  • 这个解决方案的问题是你解析了16次字符串。
  • @CasimiretHippolyte,您是绝对正确的,因为这在计算上并不高效。但是,计算效率并不是 OP 通过他的原始帖子或他的 cmets 表达的关注点之一。 :)
【解决方案3】:

一种使用拆分的方式:

String s = "[1][1][4][2][6][d][][][][a][8]";

String[] tok = s.split("(?<=\\[)(?=[0-9a-f]\\])|(?<=\\[[0-9a-f])(?=\\])");
String result = "";

for (int i=0; i<tok.length; i++) {
    if (i%2==1) {
        result += getColor(tok[i]);
    } else {
        result += tok[i];
    }
}

【讨论】:

    【解决方案4】:

    您可以使用以下正则表达式进行匹配:

    \[([^\s])\]

    然后替换为[x]

    正则表达式演示

    http://regex101.com/r/mH4mH1

    【讨论】:

    • 这可行,但我需要用颜色代码替换每个数字。所以每个不同的数字会有不同的颜色代码。
    • @Snhp9,您能否在您的代码中包含一个示例
    【解决方案5】:

    您可能想要做的一个解决方案是创建一个正则表达式模式,然后从中创建一个匹配器并使用它来替换字符串中的字符。

    以下是类的两个 API,它们有望提供您需要的所有信息(包括不同的正则表达式符号的含义)。

    http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html

    如果您对如何使用这些类有任何疑问,请从 google 获得很好的资源: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html http://www.tutorialspoint.com/java/java_regular_expressions.htm

    看到其他人的答案,这可能是它在调用 String.replaceAll() 时在后端所做的事情,因此这不再是最佳解决方案。我会在他们的回答中说出我将要说的话作为评论,但我的声誉很低。您可以做的是为每个代码设置多个替换语句。

    s = s.replaceAll("\\[1\\]", "[x]"));
    s = s.replaceAll("\\[2\\]", "[y]"));
    s = s.replaceAll("\\[3\\]", "[z]"));
    

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2014-10-28
      • 1970-01-01
      • 2014-04-09
      • 2018-01-07
      • 2019-07-25
      相关资源
      最近更新 更多