gpt4 book ai didi

Java Regex BBCode 不会取代,但

转载 作者:行者123 更新时间:2023-12-01 13:39:32 26 4
gpt4 key购买 nike

我将使用以下方法来替换html链接的特殊BB代码

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

但是当涉及到替换以下bbcode时,

:D

O:-)

:-[

:o)

:~(

:xx(

:-]

:-(

^3^

@_@

:哦

:)

:P

;-)

???

?_?

Z_Z

结果不是右括号并且:<-- 无法识别

我应该如何覆盖正则表达式功能代码并将上述图标列表替换为 html 图像链接?

我目前正在使用这个字符串数组,但是它出现以下错误错误:错误:未指定资源类型(位于“^index_6”,值为“@_@”)。

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

最佳答案

使用引用

您可以使用模式pattern = Pattern.compile(Pattern.quote(bbcode ));在您的代码中而不是 Pattern.compile("\\"+bbcode );

试试这个代码:

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

引用stackoverflow 了解更多详情。

关于Java Regex BBCode 不会取代,但,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20944724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com