Snhp9
Snhp9

Reputation: 529

How to replace contents of a regex

My brain just may not be working tonight but I need some help.

I have this string

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

And I want to replace anything that is in the bracket with a different character. To simplify lets just say x.

It wont be x however. I will be running this though a function to change each number to a color code.

So it will look like this when done. [x][x][x][ ][ ][ ][ ][ ][ ][x][x][x][ ][ ]

I have tried some ridiculous string.replace() function but to no avail.

EDIT: Each number I run this this function

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

Upvotes: 1

Views: 80

Answers (5)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

A way using split:

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];
    }
}

Upvotes: 1

merlin2011
merlin2011

Reputation: 75545

Here is a solution using look-aheads.

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

Based on your comment on devnull's answer, it sounds like you actually want a mapped replacement:

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

Note that this solution assumes that ChatColor has a reasonable toString method.

Upvotes: 1

Nathaniel Payne
Nathaniel Payne

Reputation: 2827

As a start, why not pull out each individual character on the string, apply some function to change the character to fit some color code that you have specified, and reassemble the string afterwords. You could run the loop within a loop and do this as many times as you wanted (what I mean, is put the bottom for loop into another for loop depending on the number of iterations the process was going to take and the various conditions). As an example, you have:

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

Let us next loop through each of the characters, and at each character c, replace the characters based on some conditions. There are multiple options here for the replacement and I just left comments in the relevant places I might try it. Here is the proposed for loop.

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

}

Upvotes: 1

robertzi7
robertzi7

Reputation: 26

One solution that you might want to do is to create a regex Pattern, then create a Matcher from it and use that to replace the characters in the string.

Here are the two APIs for the classes which hopefully give all the information that you need (including what the different regex symbols mean).

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

Good sources from google if you have any questions on how to use these classes: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html http://www.tutorialspoint.com/java/java_regular_expressions.htm

Seeing the other's answers, this might be what it doing in the backend when String.replaceAll() is being called, so this is no longer the best solution. I would say what I'm about to say as a comment in their answers, but my reputation is to low. What you could do is have more than one replace statement for each code.

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

Upvotes: 0

sshashank124
sshashank124

Reputation: 32189

You can use the following regex to match:

\[([^\s])\]

and then replace with [x].

Regex Demo

http://regex101.com/r/mH4mH1

Upvotes: 0

Related Questions