英文:
How does golang do with complicated regexp pattern
问题
我想在Go语言中替换由shell输出的所有ANSI/VT100/xterm控制序列。我在这里搜索了这个正则表达式模式链接,但它不起作用!以下是错误信息:
panic: regexp: Compile(`[\b]`): error parsing regexp: invalid escape sequence: `\b`
让我更清楚一些。我想在Go语言中实现以下代码:
#!/usr/bin/env perl
while (<>) {
s/ \e[ #%()*+\-.\/]. |
(?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
(?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
(?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
\e.|[\x80-\x9f] //xg;
1 while s/[^\b][\b]//g; # remove all non-backspace followed by backspace
print;
}
英文:
I wanna replace all ANSI/VT100/xterm control sequences output by shell in golang And i searched this regexp pattern here, but it doesn't work!
here is error:
panic: regexp: Compile(`[\b]`): error parsing regexp: invalid escape sequence: `\b`
Let me make it more clear. I wanna accomplish the following code in golang:
#!/usr/bin/env perl
while (<>) {
s/ \e[ #%()*+\-.\/]. |
(?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
(?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
(?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
\e.|[\x80-\x9f] //xg;
1 while s/[^\b][\b]//g; # remove all non-backspace followed by backspace
print;
}
答案1
得分: 2
使用[^\x08]\x08
代替。
在**正则表达式语法**中,不支持包含[\b]
和\e
的转义序列。
退格符:在其他正则表达式引擎中,转义序列\b
在字符类内有不同的含义:它匹配退格字符(十六进制08)。但是,你可以将其转义为\x08
。
转义符:类似地,对于转义符\e
(十六进制1B),请使用\x1b
。
此外,如果你想匹配任何控制字符,可以使用[[:cntrl:]]
,它匹配[\x00-\x1F\x7F]
。
英文:
Use [^\x08]\x08
instead.
[\b]
and \e
are not included in the supported Escape sequences stated in regexp syntax.
Backspace: For other regex flavors, the escape sequence \b
has a different meaning inside a character class: it matches the backspace character (hex 08). However, you may escape it as \x08
.
Esc: Similarly, for the Escape \e
(hex 1B), use \x1b
.
Also, if you want to match ANY control character, you may use [[:cntrl:]]
, which matches [\x00-\x1F\x7F]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论