繁体   English   中英

如何在模式匹配之前使用正则表达式将字符串拆分为多行

[英]How to split a string into multiple lines using regex before the pattern match

我有一个包含以下格式的 swift 数据的文件,需要使用 python 中的正则表达式将其拆分为多行。 原始文件:

ID        Information

1         :20:Test1  :25:test2:28C:test3

所需的 Output:

ID  Information

1     :20:Test1  
1     :25:test2  
1     :28C:test3

使用记事本++,我可以使用将“信息”列分成多行

查找: ^:[0-9]{2}:|\s:[0-9]{2}:|\s:[0-9]{2}[A-Za-z]{1}:

替换: \n$0

需要使用 python 复制相同的内容。 到目前为止,我尝试了以下代码,但结果不包含该模式。 它在模式匹配后分裂:

import re

s = ':20:Test1  :25:test2:28C:test3'

l = re.compile('^:[0-9]{2}:|\s:[0-9]{2}:|\s:[0-9]{2}[A-Za-z]{1}:').split(s)

结果: ['', 'Test1 ', 'test2 ', 'test3']

拆分字符串时,结果还应包含正则表达式模式。

这个模式怎么样:

import re

s = ':20:Test1  :25:test2:28C:test3'

p = re.compile('(:[0-9A-z]{1,3}:)([0-9A-z]+)')

print(p.findall(s))
#[(':20:', 'Test1'), (':25:', 'test2'), (':28C:', 'test3')]

鉴于您有多种类型的 output,使用正则表达式的小逻辑可能更容易:

s='''\
ID        Information

1         :20:Test1  :25:test2:28C:test3'''
    

import re 

for line in s.splitlines():
    if m:=re.search(r'^(\d+)([ \t]+)(:.*)',line):
        data=re.findall(r'(:[^:]+:[^:]+(?=:|$))', m.group(3))
        for e in data:
            print(m.group(1)+m.group(2)+e.rstrip())
    else:
        print(line)     

印刷:

ID        Information

1         :20:Test1
1         :25:test2
1         :28C:test3

如所写,仅 Python 3.8+。 如果您想要更早的 Python 3.X:

for line in s.splitlines():
    m=re.search(r'^(\d+)([ \t]+)(:.*)',line)
    if m:
      ...

您可以使用

import re
text = """ID        Information

1         :20:Test1  :25:test2:28C:test3"""

valid_line_rx = r'^(\d+\s*)(:\d{2}[A-Za-z]?:.*)'
print( re.sub(valid_line_rx, lambda m:
  "\n".join(["{}{}".format(m.group(1),x) for x in re.split(r'(?!^)(?=:\d{2}[A-Za-z]?:)', m.group(2))]),
  text, 
  flags=re.M)
)

参见Python 演示,output:

ID        Information

1         :20:Test1  
1         :25:test2
1         :28C:test3

^(\d+\s*)(:\d{2}[A-Za-z]?:.*)正则表达式匹配

  • ^ - 行首(由于re.M标志)
  • (\d+\s*) - 第 1 组:一个或多个数字,然后是 0 个或多个空格
  • (:\d{2}[A-Za-z]?:.*) - 第 2 组: : ,两位数,一个可选字母和 aa :以及尽可能多的除换行符以外的任何 0 个或多个字符.

(??^)(:=?\d{2}[A-Za-z]::)正则表达式匹配不是字符串开头的位置,并且紧随其后的是: 、2位数字、一个可选字母和a : ,并且此模式用于拆分上述正则表达式匹配的 Group 2 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM