首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将string方法应用于Python中的正则表达式

如何将string方法应用于Python中的正则表达式
EN

Stack Overflow用户
提问于 2016-06-29 09:49:44
回答 3查看 48关注 0票数 0

我有一个打折文件,有点坏了:链接和图片太长了,里面有断线。我想从他们身上去掉断线。

示例:

发自:

代码语言:javascript
复制
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

![https://diasporafoundation.org/assets/pages/about/network-
distributed-e941dd3e345d022ceae909beccccbacd.png](data/images/network-
distributed-e941dd3e345d022ceae909beccccbacd.png)

_A pretty decentralized network (Source: <https://diasporafoundation.org/>)_

至:

代码语言:javascript
复制
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

![https://diasporafoundation.org/assets/pages/about/network-distributed-e941dd3e345d022ceae909beccccbacd.png](data/images/network-distributed-e941dd3e345d022ceae909beccccbacd.png)

_A pretty decentralized network (Source: <https://diasporafoundation.org/>)_

正如您在这个片段中所看到的,我成功地将所有链接和图像匹配为正确的模式:https://regex101.com/r/uL8pO4/2

但是现在,在我用正则表达式捕获的内容上,使用string.trim()方法(如)的语法是什么呢?

就目前而言,我被困在这:

代码语言:javascript
复制
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'])

编辑:我更新了这个示例,以便更明确地说明我的问题.

谢谢你的回答

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-29 14:55:58

好吧,我终于找到了我要找的东西。通过下面的代码片段,我可以用regex捕获一个字符串,然后对每个字符串应用处理。

代码语言:javascript
复制
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'])

谢谢你的回答,如果我的问题不够明确的话,很抱歉。

票数 0
EN

Stack Overflow用户

发布于 2016-06-29 09:51:10

带材的工作原理与trim功能相似。由于您需要修整新的线条,请使用条带(‘\n’),

代码语言:javascript
复制
fin.readline.strip('\n')
票数 0
EN

Stack Overflow用户

发布于 2016-06-29 10:26:26

这也将起作用:

代码语言:javascript
复制
>>> s = """
...    ![https://diasporafoundation.org/assets/pages/about/network-
... distributed-e941dd3e345d022ceae909beccccbacd.png](data/images/network-
... distributed-e941dd3e345d022ceae909beccccbacd.png)
... """

>>> new_s = "".join(s.strip().split('\n'))
>>> new_s
'![https://diasporafoundation.org/assets/pages/about/network-distributed-e941dd3e345d022ceae909beccccbacd.png](data/images/network-distributed-e941dd3e345d022ceae909beccccbacd.png)'
>>> 

通常情况下,内置的字符串函数可以执行,而且比计算regexes更容易阅读。在这种情况下,条带移除前导和尾随空间,然后split返回换行符之间的项列表,并将它们合并到一个字符串中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38096048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档