我正在尝试从字符串中提取文本和十六进制颜色。
目前符号“>”有一个小问题
这是我目前得到的代码;
package main.cache;
import java.util.Arrays;
import java.util.regex.Pattern;
public class Main {
public static void extract(String string) {
final String STARTS_WITH_COLOR_LITERAL = "^[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}";
final Pattern pattern = Pattern.compile(STARTS_WITH_COLOR_LITERAL);
Object[] objects = Arrays.stream(string.split("<col=")).filter(part -> pattern.matcher(part).find()).toArray();
String name;
String color = null;
for (int i = 0; i < objects.length; i++) {
String[] line = objects[i].toString().split(">");
if (line.length == 1) {
name = line[0];
} else {
color = line[0];
name = line[1];
}
System.out.println("Color " + color + ", name " + name);
}
}
public static void main(String[] args) {
extract("something before<col=ff00ff>mercides> car<col=ffff00>plates");
}
}
例如传递这个参数时,输出应该是
Color null, name something before
Color ff00ff, name mercides> car
Color ffff00, name plates
我目前得到的输出是
Color null, name something before
Color ff00ff, name mercides
Color ffff00, name plates
撒科打诨
相关分类