Golang如何处理复杂的正则表达式模式?

huangapple go评论113阅读模式
英文:

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`

code sample

Let me make it more clear. I wanna accomplish the following code in golang:

  #!/usr/bin/env perl
  while (&lt;&gt;) {
      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].

huangapple
  • 本文由 发表于 2015年11月12日 13:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33664781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定