如何匹配不是4的倍数的空格?

时间:2014-08-15 02:33:35

标签: python regex notepad++

我使用notepad ++重新格式化Python脚本,但有些行没有缩进4(或8,12,16等)空格。

所以我需要匹配连续的前导空格(即每行开头的缩进),不是4的多个,即数量的空格1,2,3,5,6,7,9,10,11等

e.g。

>>>   a = 1      # match this, as there're 3 spaces at the beginning
>>>       b = a  # match this too, as indent by 7 spaces
>>>    c = 2     # but not this, since it's indented exactly by 4 spaces
>>>        d = c # not this either, since indented by 8 spaces

我能够使用以下内容匹配4个中的空格:

^( {16}| {12}| {8}| {4})

然后我尝试将其与此相反:

^[^( {16}| {12}| {8}| {4})]

但它只匹配空行或行开头与字符,而不是我想要的。

我是正则表达式的完全新手,但我已经搜索了几个小时而没有运气。我知道我总是可以匹配列出的所有非多数的4个数字,但我希望有人可以提供帮助并提供一种不那么繁琐的方法。

感谢。

更新1

使用正则表达式(@ user2864740)

^(?:\s{4})*\s{1,3}\S

或(@alpha bravo)

^(?!(\s{4})+\S)(.*)

匹配非多重4个缩进,以及带有4个(8,16等)空格的空行以及跟随它们的第一个非空行的第一个字符。

e.g.(在regex101.com上)

如何避免匹配上述示例中描述的这些情况?

3 个答案:

答案 0 :(得分:9)

字符类只能包含..一组字符,因此[^..]不适合一般否定。正则表达式[^( {16}| {12}| {8}| {4})]相当于[^( {16}|284],它将匹配未列出的每个字符

现在,要匹配而不是 4个空格的倍数与查找n mod 4 = {1, 2, 3}(或除 n mod 4 = 0之外的任何)空格相同。这可以通过以下模式完成:

(?:\s{4})*\s{1,3}\S

说明:

(?:\s{4})*  - match any number of whole groups of 4 spaces and then ..
\s{1,3}     - match any count of 1, 2, or 3 spaces such that ..
\S          - they are not followed by a space

正则表达式可能需要一个尾随点(.*)或前导线锚(^),具体取决于它的使用方式。

答案 1 :(得分:2)

我可以提供一个python脚本,告诉你哪些行不正确缩进:

with open('path/to/code/file') as infile:
    for i,line in enumerate(infile,1):
        total = len(line)
        whitespace = total-len(line.lstrip(' '))
        if whitespace%4:
            print("Inconsistent indenting on line", i)

答案 2 :(得分:1)

你可以使用这种模式^(?!(\s{4})+\S)(.*) Demo