我想将ANSI Escape序列转换为IRC 颜色序列。
所以我写了一个正则表达式 1\e\[([\d;]+)?m
但是,shell_output_string.replaceFirst ("\\e\\[([\\d;]+)?m", "$1")
它将返回匹配的子字符串和其余不匹配的子字符串。
然后我写了正则表达式 2 .*\e\[([\d;]+)?m.*
,希望它可以匹配整个字符串并用匹配的子字符串替换它,但是,replaceFirst (".*\\e\\[([\\d;]+)?m.*", "$1")
返回空字符串,但是matches (".*\\e\\[([\\d;]+)?m.*")
is true
。这个正则表达式有什么问题?
以下问题与此问题非常相似:Pattern/Matcher group() to obtain substring in Java?
示例代码
import java.util.regex.*;
public class AnsiEscapeToIrcEscape
{
public static void main (String[] args)
{
//# grep --color=always bot /etc/passwd
//
//bot:x:1000:1000:bot:/home/bot:/bin/bash
byte[] shell_output_array = {
0x1B, 0x5B, 0x30, 0x31, 0x3B, 0x33, 0x31, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[01;31m^[[K (#1 - #11)
0x62, 0x6F, 0x74, // bot (#12 - #14)
0x1B, 0x5B, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[m^[[K (#15 - #20)
0x3A, 0x78, 0x3A, 0x31, 0x30, 0x30, 0x30, 0x3A, 0x31, 0x30, 0x30, 0x30, 0x3A, // :x:1000:1000: (#21 - #33)
0x1B, 0x5B, 0x30, 0x31, 0x3B, 0x33, 0x31, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[01;31m^[[K (#34 - #44)
0x62, 0x6F, 0x74, // bot (#45 - #47)
0x1B, 0x5B, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[m^[[K (#48 - #53)
0x3A, 0x2F, 0x68, 0x6F, 0x6D, 0x65, 0x2F, // :/home/ (#54 - #60)
0x1B, 0x5B, 0x30, 0x31, 0x3B, 0x33, 0x31, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[01;31m^[[K (#61 - #71)
0x62, 0x6F, 0x74, // bot (#72 - #74)
0x1B, 0x5B, 0x6D, 0x1B, 0x5B, 0x4B, // ^[[m^[[K (#75 - #80)
0x3A, 0x2F, 0x62, 0x69, 0x6E, 0x2F, 0x62, 0x61, 0x73, 0x68, // :/bin/bash (#81 - #90)
};
String shell_output = new String (shell_output_array);
System.out.println (shell_output);
System.out.println ("total " + shell_output_array.length + " bytes");
final String CSI_REGEXP = "\\e\\[";
final String CSI_SGR_REGEXP_First = CSI_REGEXP + "([\\d;]+)?m";
final String CSI_SGR_REGEXP = ".*" + CSI_SGR_REGEXP_First + ".*";
System.out.println (shell_output.replaceFirst(CSI_SGR_REGEXP_First, "$1"));
System.out.println (shell_output.replaceFirst(CSI_SGR_REGEXP, "$1"));
}
}