Python字符串拆分模式而不删除分隔符

时间:2016-02-24 12:27:36

标签: python regex split

我有一个很长的字符串,每当某个图案出现时我想把它分成更小的毒刺:(在下面的情况123我的)

my_str = '123 my string is long 123 my string is very long 123 my string is so long'

我希望结果是:

result = ['123 my string is long ', '123 my string is very long ', '123 my string is so long ']

字符串的长度未知。而且我不想从主字符串中删除任何内容。

3 个答案:

答案 0 :(得分:7)

你也可以使用前瞻性正则表达式:

import re
re.split(r'.(?=123 my)', my_str)
=>
['123 my string is long',
 '123 my string is very long',
 '123 my string is so long']

答案 1 :(得分:3)

您可以拆分分隔符,然后使用列表解析将其重新添加:

my_str = '123 my string is long 123 my string is very long 123 my string is so long'
delimiter = '123 my'
result = ['{}{}'.format(delimiter, s) for s in my_str.split(delimiter) if s]
print(result)

<强>输出

['123 my string is long ', '123 my string is very long ', '123 my string is so long']

我不知道最后一个列表项中的尾随空格来自所需输出的位置,它不在原始字符串中,因此在结果中应该不存在。

请注意,这只适用于分隔符从字符串

开始处开始的情况

答案 2 :(得分:0)

所以......有点hacky,但你可以分两步完成

Collecting ant
Using cached 

https://files.pythonhosted.org/packages/7e/9a/672711e3e9e264d7eee6048945f497d9fa9b311877f64fb74a7f297837da/ant-0.1.0.tar.gz

Collecting distribute (from ant)
 Using cached https://files.pythonhosted.org/packages/5f/ad/1fde06877a8d7d5c9b60eff7de2d452f639916ae1d48f0b8f97bf97e570a/distribute-0.7.3.zip

    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\setuptools\__init__.py", line 2, in <module>
        from setuptools.extension import Extension, Library
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\setuptools\extension.py", line 5, in <module>
        from setuptools.dist import _get_unpatched
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\setuptools\dist.py", line 7, in <module>
        from setuptools.command.install import install
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\setuptools\command\__init__.py", line 8, in <module>
        from setuptools.command import install_scripts
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\setuptools\command\install_scripts.py", line 3, in <module>
        from pkg_resources import Distribution, PathMetadata, ensure_directory
      File "C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\pkg_resources.py", line 1518, in <module>
        register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\97252\AppData\Local\Temp\pycharm-packaging\distribute\

我是这样做的:

 1. Find and replace all matches with (the match plus some custom character sequence or "\n").

 2. Split the new string by the custom sequence.