0

I am going to use the following method to replace special BB Codes for html links

 public String replace(String text , String bbcode , String imageLocation ){


        StringBuffer imageBuffer = new StringBuffer (""); 
        Pattern pattern = Pattern.compile("\\"+bbcode );
        Matcher matcher = pattern.matcher(text);    
        StringBuilder builder = new StringBuilder();
        int i = 0;
        while (matcher.find()) {
            //String orginal = replacements.get(matcher.group(1));
            imageBuffer.append("<img src=\"" + imageLocation + "\" />");
            String replacement = imageBuffer.toString();
            builder.append(text.substring(i, matcher.start()));
            if (replacement == null) {
                builder.append(matcher.group(0));
            } else {
                builder.append(replacement);
            }
            i = matcher.end();
        }
        builder.append(text.substring(i, text.length()));
        return builder.toString();
    }

but when it comes to replacing the following bbcodes ,

:D

O:-)

:-[

:o)

:~(

:xx(

:-]

:-(

^3^

@_@

:O

:)

:P

;-)

???

?_?

Z_Z

It turns out to be not closing bracket and : <-- not recognized

How should I override the regex functional code and replace the abovementioned list of icons as html image links?

I am currently using this string array but It comes out with the following error error: Error: No resource type specified (at '^index_6' with value '@_@').

 <string-array name="hkgicon_array">
        <item>[369]</item>
        <item>#adore#</item>
        <item>#yup#</item>
        <item>#ass#</item>
        <item>:-(</item>
        <item>^3^</item>
        <item>@_@</item>
    </string-array>
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141

1 Answers1

1

USE QUOTE

You can use Pattern pattern = Pattern.compile(Pattern.quote(bbcode )); in your code instead of Pattern.compile("\\"+bbcode );.

Try this code:

public static String replace(String text , String bbcode , String imageLocation ){


        StringBuffer imageBuffer = new StringBuffer (""); 
        Pattern pattern = Pattern.compile(Pattern.quote(bbcode ));
        Matcher matcher = pattern.matcher(text);    
        StringBuilder builder = new StringBuilder();
        int i = 0;
        while (matcher.find()) {
            //String orginal = replacements.get(matcher.group(1));
            imageBuffer.append("<img src=\"" + imageLocation + "\" />");
            String replacement = imageBuffer.toString();
            builder.append(text.substring(i, matcher.start()));
            if (replacement == null) {
                builder.append(matcher.group(0));
            } else {
                builder.append(replacement);
            }
            i = matcher.end();
        }
        builder.append(text.substring(i, text.length()));
        return builder.toString();
    }

Refer stackoverflow for more details .

Community
  • 1
  • 1
Sujith PS
  • 4,776
  • 3
  • 34
  • 61