正则表达式用空格替换控制台代码 [英] regex to replace console code with whitespaces

查看:59
本文介绍了正则表达式用空格替换控制台代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为使用控制台代码,并且在优雅地处理 ESC H 序列时遇到了一些问题.

I'm writing some Python tests for a console application that uses console codes, and I'm having some problem gracefully handling the ESC H sequence.

我有 s = r'\ x1b [12; 5H \ nSomething'输入字符串,我想用 Something 替换它.我正在尝试使用以下正则表达式:

I have the s = r'\x1b[12;5H\nSomething' input string, I'd like to replace it with Something. I'm trying to use the following regex:

re.sub(r'\ x1b \ [([0-9,AZ] {1,2};([0-9] {1,2})H)',r'\ 2',s)

当然会创建 5Something .

我想要的是

re.sub(r'\ x1b \ [([0-9,AZ] {1,2};([0-9] {1,2})H)',''*(int(r'\ 2')-1),s)

创建的空间比第二个捕获组的空间少一.

Which is to create one less than the number of spaces of the second capture group.

如果有一种方法可以简单地将使用 print 时的打印结果呈现为字符串,我也将非常高兴:

I'd also be very happy if there was a way to simply render in a string what I get when I use print(s):

    Something

我正在使用Python 3.

I'm using Python 3.

非常感谢!

推荐答案

使用

import re
s = r'\x1b[12;5H\nSomething'
pattern = r'\\x1b\[[0-9A-Z]{1,2};([0-9]{1,2})H\\n'
print(re.sub(pattern, lambda x: ' '*(int(x.group(1))-1), s))

请参阅的Python证明.请参见正则表达式证明.

EXPLANATION

--------------------------------------------------------------------------------
  \\                       '\'
--------------------------------------------------------------------------------
  x1b                      'x1b'
--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  [0-9A-Z]{1,2}            any character of: '0' to '9', 'A' to 'Z'
                           (between 1 and 2 times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  ;                        ';'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [0-9]{1,2}               any character of: '0' to '9' (between 1
                             and 2 times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  H                        'H'
--------------------------------------------------------------------------------
  \\                       '\'
--------------------------------------------------------------------------------
  n                        'n'

这篇关于正则表达式用空格替换控制台代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆