首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当细分出一个通用表达式时,只需要提取数字

当细分出一个通用表达式时,只需要提取数字
EN

Stack Overflow用户
提问于 2017-12-13 01:56:25
回答 1查看 56关注 0票数 0

以下是示例文本:

initiated to address the deviation to SOP-020583v11.0 Section SOP-016248v2.0 john doe, john doe SOP-020583 fake text, this is all fake

理想情况下,案文应如下所示:

initiated to address the deviation to 020583 Section 016248 john doe, john doe 020583 fake text, this is all fake

下面是我到目前为止掌握的代码:

代码语言:javascript
复制
def dashrepl(matchobj):
    print (type(matchobj))
    return re.findall('[0-9]',matchobj)

re.sub(SOP, dashrepl, long_desc_text[22])

但我得到了以下错误:

代码语言:javascript
复制
TypeError: expected string or buffer

编辑更新的内容:

long_desc_text[22]

SOP-020583v11.0 Section 8.4.On 17Jan2016 at ATO Site, SOP-016248v2.0 was due for periodic review but the periodic SOP-016248 revision is not tied to any change control records. SOP-020583 tied to a change control record" and notified ID63718 notifiedID22359 of the event. SOP-020583v11.0, fake text fake text

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-13 05:22:42

这是我的密码:

代码语言:javascript
复制
import re

test = "initiated to address the deviation to SOP-020583v11.0 Section SOP-016248v2.0 john doe, john doe SOP-020583 fake text, this is all fake"

regexp = r"SOP-(\d+)(?:v\d+\.\d)?"

test = re.subn(regexp, r"\1", test)

print test[1]

它产生:

启动解决020583条偏离016248节的约翰·多伊,约翰·多伊020583假文本,这都是假的

使用python函数"subn“查找并用指定的字符串替换模式的所有示例--在本例中是第一个捕获组。字符串前面的"r“将其指定为regex对象。

作为参考,我还找到了这个link

希望这能有所帮助。

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

https://stackoverflow.com/questions/47784384

复制
相关文章

相似问题

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