- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Python 中的正则表达式来匹配 str 中的数字。我的愿望是捕获可能有千位分隔符(对我来说,逗号或空格)或可能只是一串数字的数字。下面显示了我的正则表达式捕获的内容
>>> import re
>>> test = '3,254,236,948,348.884423 cold things, ' + \
'123,242 falling birds, .84973 of a French pen , ' + \
'65 243 turtle gloves, 8 001 457.2328009 units, and ' + \
'8d523c.'
>>> matches = re.finditer(ANY_NUMBER_SRCH, test, flags=re.MULTILINE)
>>> for match in matches:
... print (str(match))
...
<_sre.SRE_Match object; span=(0, 24), match='3,254,236,948,348.884423'>
<_sre.SRE_Match object; span=(27, 34), match='123,242'>
<_sre.SRE_Match object; span=(37, 43), match='.84973'>
<_sre.SRE_Match object; span=(46, 52), match='65 243'>
<_sre.SRE_Match object; span=(55, 72), match='8 001 457.2328009'>
<_sre.SRE_Match object; span=(73, 74), match='8'>
<_sre.SRE_Match object; span=(75, 78), match='523'>
这是我想要的匹配行为。现在,我想获取每个匹配的数字并删除千位分隔符(','
或 ' '
)(如果存在)。这应该留给我
'3254236948348.884423 cold things, ' + \
'123242 falling birds, .84973 of a French pen ,' + \
'65243 turtle gloves, 8001457.2328009 units, ' + \
'and 8d523c.'
基本上,我有一个正则表达式来捕获数字。该正则表达式用于多个地方,例如查找美元金额、获取序数……因此,我将正则表达式命名为 ANY_NUMBER_SRCH
。
我想做的事情如下:
matches = some_method_to_get_all_matches(ANY_NUMBER_SRCH)
for match in matches:
corrected_match = re.sub(r"[, ]", "", match)
change_match_to_corrected_match_in_the_test_string
事实上,我无法使用替换组。如果您只想查看正则表达式,可以查看 https://regex101.com/r/AzChEE/3基本上,我的正则表达式的一部分如下
r"(?P<whole_number_w_thous_sep>(?P<first_group>\d{1,3})(?P<thousands_separator>[ ,])(?P<three_digits_w_sep>(?P<three_digits>\d{3})(?P=thousands_separator))*(?P<last_group_of_three>\d{3})(?!\d)"
我将在没有“滚动线”的情况下表示它:
(r"(?P<whole_number_w_thous_sep>(?P<first_group>\d{1,3})"
"(?P<thousands_separator>[ ,])"
"(?P<three_digits_w_sep>(?P<three_digits>\d{3})"
"(?P=thousands_separator))*"
"(?P<last_group_of_three>\d{3})(?!\d)")
正则表达式引擎不会保留重复的 two_digits_with_separator
,因为 *
用于重复捕获组。
我确信有一种方法可以使用 _sre.SRE_Match 对象
的 span
部分。然而,这会非常复杂,而且我正在处理包含数千到数十万字符的字符串。 有没有一种简单的方法可以在 re.match
或 re.iter
或使用任何其他方法之后执行 re.sub
找到数字模式?
@abarnert 给了我正确的答案 - 使用 lambda 函数。我的评论在 @abarnert's answer ,以“已验证!”开头显示所有步骤。以防万一该评论出现了损坏的链接, .
<小时/>顺便说一句,我已经看过SO上的这些问题( replace portion of match , extract part of a match , replace after matching pattern , repeated capturing group stuff ),但它们只是展示了如何使用替换组。我还尝试使用 re.finditer
,如下所示,结果如下。
>>> matches = re.finditer(lib_re.ANY_NUMBER_SRCH, test, flags=re.MULTILINE)
>>> for match in matches:
... print ("match: " + str(match))
... corrected_match = re.sub(r"[, ]", "", match)
... print ("corrected_match: " + str(corrected_match))
...
match: <_sre.SRE_Match object; span=(0, 24), match='3,254,236,948,348.884423'>
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
>>> print ("corrected_match: " + str(corrected_match))
<小时/>
万一 regex101.com link 出现问题,这是巨大的正则表达式:
ANY_NUMBER_SRCH = r"(?P<number_capture>(?P<pre1>(?<![^0-9,.+-])|)(?P<number>(?P<sign_symbol_opt1>(?<![0-9])[+-])?(?P<whole_number_w_thous_sep>(?P<first_group>\d{1,3})(?P<thousands_separator>[ ,])(?P<three_digits_w_sep>(?P<three_digits>\d{3})(?P=thousands_separator))*(?P<last_group_of_three>\d{3})(?!\d)|(?P<whole_number_w_o_thous_sep>\d+))(?P<decimal_separator_1>[.])?(?P<fractional_w_whole_before>(?<=[.])(?P<digits_after_decimal_sep_1>\d+))?(?P<post1>(?<![^0-9,.+-])|)|(?P<pre2>(?<![^0-9,.+-])|)(?P<fractional_without_whole_before>(?P<sign_symbol_opt2>(?<![0-9])[+-])?(?P<decimal_separator_2>[.])(?P<digits_after_decimal_sep_2>\d+)))(?P<post2>(?<![^0-9,.+-])|))"
最佳答案
我认为您没有任何理由不能只使用 re.sub
而不是这里的 re.finditer
。您的 repl
对每个匹配应用一次,并返回用 string
中的 repl
替换每个 pattern
的结果,这正是您想要的。
我实际上无法运行你的示例,因为复制和粘贴 test
会给我一个语法错误,而复制和粘贴 ANY_NUMBER_SRCH
会给我一个编译正则表达式的错误,并且我不想陷入困境,尝试修复所有错误,其中大多数错误甚至可能不在您的真实代码中。那么让我举一个更简单的例子:
>>> test = '3,254,236,948,348.884423 cold things and 8d523c'
>>> pattern = re.compile(r'[\d,]+')
>>> pattern.findall(test) # just to verify that it works
['3,254,236,948,348', '884423', '8', '523']
>>> pattern.sub(lambda match: match.group().replace(',', ''), test)
'3254236948348.884423 cold things and 8d523c'
显然,您的 repl
函数比仅删除所有逗号要复杂一些,而且您可能希望将其 def
置于外线,而不是而不是尝试将其塞入 lambda 中。但无论您的规则是什么,如果您将其编写为一个函数,该函数接受 match
对象并返回您想要的字符串来代替该匹配对象,您只需将该函数传递给 sub
.
关于python - 匹配后re.sub。重复匹配组的所有实例,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51640496/
有没有一种方法可以使用标准类型构造函数(例如 int、set、dict、list、tuple 等)以用户定义的方式将用户定义类的实例强制转换为其中一种类型?例如 class Example:
我知道这个问题在Stackoverflow中有很多问题,但是即使有很多答案,这些答案也帮不了我什么,也没有找到答案。 在我的WebAPP中,它可以正常工作,但是当我将其转换为API时,它失败了(主题标
这个问题已经有答案了: Why does the ternary operator unexpectedly cast integers? (3 个回答) 已关闭 9 年前。 最近遇到一个Java的陷
我尝试使用 FirebaseApp.configure() 配置 Firebase,但遇到以下崩溃: *** Terminating app due to uncaught exception 'c
我有一个自连接员工实体类,其中包含与其自身相关的 id、name 和 ref 列。我想创建它的新实例并将其保存到数据库。 首先我创建了一个 Employee 类的实例并将其命名为 manager。然后
我有一个用于添加新公寓的表单,在该表单中我有一个下拉列表,用户可以在其中选择负责的人员。 显然,当您从下拉列表中选择并尝试保存公寓时,我的应用程序认为该人已被修改。它给了我下面的错误,指示我应该首先保
从 Visualforce 页面,我需要检索我们组织的 salesforce 实例的 URL,而不是 Visual Force URL。 例如我需要https://cs1.salesforce.com
我遇到了一些可能的问题答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8 的问题。所以这曾经在以前的版本下工作,我已经四处搜索了为什么它在这个新版本中出现了
似乎一遍又一遍地问这个问题,我仍然找不到解决我问题的答案。我在下面有一个域模型。每个新创建或更新的“安全用户”都需要我确保其具有配置文件,如果没有,则创建一个新的配置文件并分配给它。 配置文件的要求相
我很难调试为什么 JPA 不级联我的 @ManyToMany 关系。我发现的所有答案都与缺少级联语句有关。但我确实拥有它们并且仍然得到: Caused by: org.hibernate.Transi
Play 服务 API 表明有一个叫做 Instance ID 的东西 但是,在 Android Studio 中包含以下内容后,我无法导入 InstanceID 类 compile "com.goo
我正在使用 Seam 框架。我有 2 个实体: 请求.java @Entity @Table(name = "SRV_REQUEST") public class Request { private
This question处理构建一个适当的Monad来自单子(monad)的实例,但仅在某些约束下 - 例如Set .诀窍是将其包装成 ContT ,它将约束推迟到包装/展开其值。 现在我想对 Ap
我正在尝试执行此查询: StringBuffer sb = new StringBuffer(); sb.append("select p from PointsEntity p " + "where
我试图了解是否可以更改我的 hibernate 配置并使用单个 MySQL 实例(而不是我当前拥有的多个 MySQL 实例): 我有一个使用 hibernate 的 Java 应用程序,与 2 个模式
我有一个选项卡滑动布局,其中包括四个选项卡,每个选项卡都有自己的布局和 fragment ,在我的主要 Activity 布局中,viewpager 参与更改选项卡。特定 View (选项卡)在应用程
我看到很多帖子声称他们正在运行 MySql 的 RDS 实例,但无法连接到该实例,但我没有运行 RDS。 我使用 EC2 实例来托管我的 WordPress 博客,该博客是使用 Web 平台安装程序安
因为我在我的 ec-2 实例上的 python 虚拟环境中运行应用程序( Airflow ),并且我想在同一个 ec2 实例上的默认 python 环境中运行命令,所以我认为 ssh 到我自己的实例更
这个问题已经有答案了: How to fix the Hibernate "object references an unsaved transient instance - save the tra
例子: run APP1 .. ... run APP1 ... run APP2 如何在 APP2 中对 Vue 说我需要调用 APP1?
我是一名优秀的程序员,十分优秀!