简体   繁体   English

BASH控制序列解析器还是正则表达式?

[英]BASH control sequences parser or regex?

I've stumbled across this topic because I'm developing a console app, currently on a Windows machine using Cygwin to run it (but it will have to run in Linux BASH too at some stage). 我偶然发现了这个主题,因为我正在开发一个控制台应用程序,当前在使用Cygwin的Windows计算机上运行它(但是在某些阶段它也必须在Linux BASH中运行)。

Initially all I want to do is apply colour, so I've found out about all these "(ESC)[31m... (ESC)[1m" type directives... for foreground and background colours. 最初,我只想应用颜色,因此我发现了所有这些用于前景和背景颜色的“(ESC)[31m ...(ESC)[1m””类型指令...)。

But in the course of testing I also want to be able to strip such codes and just get to the "non-markup" text. 但是在测试过程中,我也希望能够剥离这样的代码,然后进入“非标记”文本。 With other forms of markup (HTML notably) there are tools available. 对于其他形式的标记(尤其是HTML),有可用的工具。 Is there anything like this for these console codes? 这些控制台代码是否有类似的内容? Googling around I found something in C, something in Python, but nothing in Java. 在谷歌搜索中,我发现了C中的某些东西,Python中的某些东西,而Java中却没有。

I think the codes involved here (for Cygwin and BASH) are the "ANSI" Control codes... but I'm not sure. 认为这里涉及的代码(针对Cygwin和BASH)是“ ANSI”控制代码...但是我不确定。 Could someone maybe confirm this? 有人可以确认吗?

Failing a full-blown parser, a regex covering all cases would be helpful. 如果没有成熟的解析器,则涵盖所有情况的正则表达式将很有帮助。 If necessary I'll try to roll one myself from the documentation, but it'd be nice to get one off the shelf from the experts... 如有必要,我会尝试从文档中挑选一个自己,但最好从专家那里购买一个……

Have a look at the well known jansi library - especially AnsiString in there: 看看著名的jansi库-特别是其中的AnsiString

AnsiString ansiString = new AnsiString("string with escape codes");
String plainString = ansiString.getPlain();

You can also have a look at the ansi-econsole plugin for Eclipse. 您也可以查看Eclipse的ansi-econsole插件。 It's an Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output. 这是一个Eclipse插件,可以理解ANSI转义序列以给Eclipse控制台输出着色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM