我有一个打折文件,有点坏了:链接和图片太长了,里面有断线。我想从他们身上去掉断线。
示例:
发自:
See for example the
[installation process for Ubuntu
Trusty](https://wiki.diasporafoundation.org/Installation/Ubuntu/Trusty). The
project offers a Vagrant installation too, but the documentation only admits
that you know what you do, that you are a developer. If it is difficult to

_A pretty decentralized network (Source: <https://diasporafoundation.org/>)_至:
See for example the
[installation process for Ubuntu Trusty](https://wiki.diasporafoundation.org/Installation/Ubuntu/Trusty). The
project offers a Vagrant installation too, but the documentation only admits
that you know what you do, that you are a developer. If it is difficult to

_A pretty decentralized network (Source: <https://diasporafoundation.org/>)_正如您在这个片段中所看到的,我成功地将所有链接和图像匹配为正确的模式:https://regex101.com/r/uL8pO4/2。
但是现在,在我用正则表达式捕获的内容上,使用string.trim()方法(如)的语法是什么呢?
就目前而言,我被困在这:
fix_newlines = re.compile(r'\[([\w\s*:/]*)\]\(([^()]+)\)')
# Capture the links and remove line-breaks from their urls
# Something like r'[\1](\2)'.trim() ??
post['content'] = fix_newlines.sub(r'[\1](\2)', post['content'])编辑:我更新了这个示例,以便更明确地说明我的问题.
谢谢你的回答
发布于 2016-06-29 14:55:58
好吧,我终于找到了我要找的东西。通过下面的代码片段,我可以用regex捕获一个字符串,然后对每个字符串应用处理。
def remove_newlines(match):
return "".join(match.group().strip().split('\n'))
links_pattern = re.compile(r'\[([\w\s*:/\-\.]*)\]\(([^()]+)\)')
post['content'] = links_pattern.sub(remove_newlines, post['content'])谢谢你的回答,如果我的问题不够明确的话,很抱歉。
发布于 2016-06-29 09:51:10
带材的工作原理与trim功能相似。由于您需要修整新的线条,请使用条带(‘\n’),
fin.readline.strip('\n')发布于 2016-06-29 10:26:26
这也将起作用:
>>> s = """
... 
... """
>>> new_s = "".join(s.strip().split('\n'))
>>> new_s
''
>>> 通常情况下,内置的字符串函数可以执行,而且比计算regexes更容易阅读。在这种情况下,条带移除前导和尾随空间,然后split返回换行符之间的项列表,并将它们合并到一个字符串中。
https://stackoverflow.com/questions/38096048
复制相似问题