Java “如何匹配”;“逃避”;正则表达式中的不可打印字符?

Java “如何匹配”;“逃避”;正则表达式中的不可打印字符?,java,regex,ansi-escape,Java,Regex,Ansi Escape,我发现了一个howto,,但没有一个代码,\e、\x1b、\x1b在Java中对我有效 编辑 Map<String,String> escapeMap = new HashMap<String,String>(); escapeMap.put("\\x1b[01;34m", "</span><span style=\"color:blue;font-weight:bold\">"); FileInputStream stream = new Fil

我发现了一个howto,,但没有一个代码,\e、\x1b、\x1b在Java中对我有效

编辑

Map<String,String> escapeMap = new HashMap<String,String>();
escapeMap.put("\\x1b[01;34m", "</span><span style=\"color:blue;font-weight:bold\">");
FileInputStream stream = new FileInputStream(new File("/home/ch00k/gun.output"));
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
String message = Charset.defaultCharset().decode(bb).toString();
stream.close();
String patternString = Pattern.quote(StringUtils.join(escapeMap.keySet(), "|"));
System.out.println(patternString);
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(message);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
    matcher.appendReplacement(sb, escapeMap.get(matcher.group()));
    }
matcher.appendTail(sb);
String formattedMessage = sb.toString();
System.out.println(formattedMessage);
我试图替换Linux终端命令输出的ANSI转义序列(特别是颜色序列)。 在Python中,替换模式看起来像“\x1b[34;01m”,这意味着蓝色粗体文本。同样的模式在Java中不起作用。我尝试单独替换“[34;01m”,结果成功了,所以问题是\x1b。 我正在使用Pattern.quote()进行“[”转义

编辑

Map<String,String> escapeMap = new HashMap<String,String>();
escapeMap.put("\\x1b[01;34m", "</span><span style=\"color:blue;font-weight:bold\">");
FileInputStream stream = new FileInputStream(new File("/home/ch00k/gun.output"));
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
String message = Charset.defaultCharset().decode(bb).toString();
stream.close();
String patternString = Pattern.quote(StringUtils.join(escapeMap.keySet(), "|"));
System.out.println(patternString);
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(message);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
    matcher.appendReplacement(sb, escapeMap.get(matcher.group()));
    }
matcher.appendTail(sb);
String formattedMessage = sb.toString();
System.out.println(formattedMessage);
编辑

Map<String,String> escapeMap = new HashMap<String,String>();
escapeMap.put("\\x1b[01;34m", "</span><span style=\"color:blue;font-weight:bold\">");
FileInputStream stream = new FileInputStream(new File("/home/ch00k/gun.output"));
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
String message = Charset.defaultCharset().decode(bb).toString();
stream.close();
String patternString = Pattern.quote(StringUtils.join(escapeMap.keySet(), "|"));
System.out.println(patternString);
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(message);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
    matcher.appendReplacement(sb, escapeMap.get(matcher.group()));
    }
matcher.appendTail(sb);
String formattedMessage = sb.toString();
System.out.println(formattedMessage);

因此,根据,转义字符应该与
“\u001B”
匹配,这在我的情况下确实有效。问题是,如果我使用
标记。put(“\u001B”+Pattern.quote([01;34m”),“qwe”);
,我仍然会得到上面提到的NPE。

quote()
是创建与输入字符串逐字匹配的模式。您的字符串中包含模式语言。查看quote()的输出-您将看到它试图逐字查找四个字符\x1b。

ansi转义序列的格式如下[\033[34;01m]

其中\033是ANSI字符033(oct)或1b(十六进制)或27(十进制)。您需要使用以下regexp:

Pattern p = Pattern.compile("\033\\[34;01m");

在java字符串中使用不可打印字符时,可以使用八进制(\033)或十六进制(\x1b)表示法。

正确的“转义”值regexp中的字符是
\u001B
FWIW,我一直在努力从彩色log4j文件中剥离ANSI颜色代码,对于我遇到的所有情况,这个小模式似乎都起到了作用:

Pattern.compile("(\\u001B\\[\\d+;\\d+m)+")

你能展示你的正则表达式和一个你想要匹配或排除什么的例子吗?如果你不展示代码,没有人会发现你没有想到的错误。例如:你是用引号()括起整个字符串,还是只用“[”?如果是整个字符串,那就是问题所在。这是我对你正在做的事情的第三次猜测-向我们展示会让你更快地得到一个有用的答案,并减少我们的时间。@Becuzz抱歉。我添加了我的代码。我的模式字符串看起来像:“\\x1B[01;34m”,但它不起作用。我还尝试了“\\e[1;34m”,这也不起作用。@Chook-括号需要转义(\\)。我尝试了另一种方法:Pattern Pattern=Pattern.compile(“\\x1b”+Pattern.quote([01;34m”));但它给了我一个NPE。@安德烈-我复制并粘贴了它,但在执行它时没有得到NPE。\x工作正常。请参阅。此外,转义序列不是以括号开始的。它们以转义开始。嗯……模式仍然以括号开始。