获取正则表达式中匹配的字符数

时间:2014-07-24 21:29:29

标签: python regex sublimetext3

我有一个缩进的文件,只有一个空格缩进:

Level1 with some text
 Level2 
Level1 
  Level3 and its text

我想用4个空格缩进它。我想到了一个匹配起始空间^(\s)*的正则表达式。但是,我不知道如何用&#34替换它;为找到的每个空格应用一个标签"。

我的预期输出是:

Level1 with some text
    Level2 
Level1 
        Level3 and its text

是否存在类似\t*len(\1)的内容?

编辑:我很欣赏基于Regex的解决方案,因为我的目标是在SublimeText编辑器中使用它,该编辑器具有regex的替换功能。

5 个答案:

答案 0 :(得分:4)

我不认为Python正则表达式引擎支持这一点,但由于您提到使用Sublime Text,您可以使用\G anchor,它匹配上一个匹配结束后的位置。

Find What:     (^|\G)\s
Replace With:  \t

此模式将匹配紧跟在行之前的匹配开头的任何单个空白字符。

答案 1 :(得分:1)

我认为正则表达式对于这个问题可能有点过分。为什么不尝试这样的事情(假设您的原始文本文件名为test.txt),

#new_space = '\t'
new_space = '    '
f = open( 'new.txt', 'w' )
for line in open( 'test.txt', 'r' ):
    nw = len(line) - len(line.lstrip())
    if nw != 0:
        f.write( nw*new_space + line.lstrip() )
    else:
        f.write( line ) 
f.close()

答案 2 :(得分:0)

如果您特别想要空格,请尝试使用此表达式。 s / / \ t / g; 你可以替换空间" "使用其他一些正则表达式字符以获得更大的灵活性(例如," \ s")。

在perl中它可能是..

$ i = ~s / / \ t / g; 打印" $ i \ n";

我刚注意到" python"标签。很抱歉perl示例和python中缺少一个。

答案 3 :(得分:0)

你可以这样做:

code = """Level1 with some text
 Level2 
Level1 
  Level3 and its text"""

TAB = "\t" # You could also give TAB="    " ( 4 spaces )

# Spaces at line start are replaced with TAB
code = code.replace('\n ', '\n'+TAB)

while code.find(TAB+" ") is not -1 :
    # For multilevel indentation
    code = code.replace(TAB+' ', TAB*2)

print code

输出:

Level1 with some text
    Level2 
Level1 
        Level3 and its text

答案 4 :(得分:0)

你可以使用积极的lookbehind断言:

text="""Level1 with some text
         Level2 
        Level1 
          Level3 and its text"""


re.sub(r'(?<=\s) ', r'\t', text, flags=re.M)

这将使用\t替换前面带有空格/换行符的每个空格,因此级别3将有两个选项卡,级别2将只有一个。单词之间的空格不受影响。输出:

Level1 with some text
    Level2 
Level1 
        Level3 and its text