2

I have the following text:

some cool color #12eedd more cool colors #4567aa

I want that this string will be transformed to:

some cool color #{1} more cool colors #{2}

How is it possible to do it in Java (1.6)?

What I've found so far is the regex for color: #[0-9abcdef]{3,6}

3
  • I've found the regex for color: #[0-9abcdef]{3,6} But I don't know how I can iterate over string and replace the colors. Commented Sep 11, 2013 at 12:28
  • Why not [0-9a-f] instead of [0-9abcdef]... Commented Sep 11, 2013 at 12:31
  • 1
    @crush because it makes me more fun to write it that way :-) Commented Sep 11, 2013 at 12:44

3 Answers 3

6

You can use appendReplacement and appendTail from Matcher class

String data = "some cool color #12eedd more cool colors #4567aa";
StringBuffer sb = new StringBuffer();

Pattern p = Pattern.compile("#[0-9a-f]{3,6}", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int i = 1;
while (m.find()) {
    m.appendReplacement(sb, "#{" + i++ + "}");
}
m.appendTail(sb);//in case there is some text left after last match

String replaced = sb.toString();
System.out.println(replaced);

output:

some cool color #{1} more cool colors #{2}
Sign up to request clarification or add additional context in comments.

4 Comments

This is the correct/ best answer, because it enables capture of the color values. These can then be stored for substitution, wherever the poster intends to put them. (This question is obviously about substitution.)
@ThomasW Not to mention, it iterates over the string a single time.
Should probably flag the pattern as case-insensitive, or add A-F to the pattern.
+1 for learning me something new with the appendReplacement method.
0

You can try without regex as this

    String str="some cool color #12eedd more cool colors #4567aa";
    StringBuilder sb=new StringBuilder();
    String[] arr=str.split(" ");
    int count=1;
    for(String i:arr){
       if(i.charAt(0)=='#'){
           sb.append("#{"+count+"} ");
           count++;
       }
        else {
           sb.append(i+" ");
       }
    }
    System.out.println(sb.toString());

out put:

 some cool color #{1} more cool colors #{2}

4 Comments

What if there is a # in the string that isn't followed by a hexadecimal number?
Seconded. I wouldn't consider this reliable to capture the color values, since the matching is too weak. This problem is essentially about substituting values & capturing the data -- reliably -- is an essential part of that. Discarding the color values (or not capturing them accurately) is unjustifiable information-loss -- this is rubbish code not a professional-quality solution.
I know it sounds harsh, but information-loss without a good reason is a big no-no.
it could be strengthen by actually matching the word i against the color regex pattern
0

Well may be this is not you are looking for but you have other option without using regex, this is simple with little bit complexion :)

StringBuilder q = new StringBuilder("some cool color #12eedd more cool colors #4567aa");
int i;
int j=1;
while(q.indexOf("#")>0){
    q.replace(i=q.indexOf("#"),i+7, "${"+ j++ +"}");
}
String result = q.toString().replaceAll("\\$", "#");
System.out.println(result);

Output:

some cool color #{1} more cool colors #{2}

1 Comment

Thank you! But the sign '#' must not stand always for color. The color should match the pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.