Python - Regular expressions get numbers between parenthesis(Python - 正则表达式在括号之间获取数字)
问题描述
我需要帮助创建正则表达式来获取括号之间的数字当我的值介于单词PIC"和."之间时
I need help creating a Regex to get numbers between parenthesis when my values are between word "PIC" and the "."
我得到了这些记录,需要能够提取 () 之间的值
I got this records and need to be able to extract values between ()
PIC S9(02)V9(05). I need this result "02 05"
PIC S9(04). I need this result "04"
PIC S9(03). I need this result "03"
PIC S9(03)V9(03). I need this result "03 03"
PIC S9(02)V9(03). I need this result "02 03"
PIC S9(04). I need this result "04"
PIC S9(13)V9(03). I need this result "13 03"
我尝试了以下方法,但它不起作用.
I have try the below but it doesnt work.
s = "PIC S9(02)V9(05)."
m = re.search(r"([0-9]+([0-9]))", s)
print m.group(1)
推荐答案
你可以使用 re.findall()
查找括号内的所有数字:
You can use re.findall()
to find all numbers within the parenthesis:
>>> import re
>>> l = [
... "PIC S9(02)V9(05).",
... "PIC S9(04).",
... "PIC S9(03).",
... "PIC S9(03)V9(03).",
... "PIC S9(02)V9(03).",
... "PIC S9(04).",
... "PIC S9(13)V9(03)."
... ]
>>> pattern = re.compile(r"((d+))")
>>> for item in l:
... print(pattern.findall(item))
...
['02', '05']
['04']
['03']
['03', '03']
['02', '03']
['04']
['13', '03']
其中 (
和 )
将匹配文字括号(需要用反斜杠转义,因为它们具有特殊含义).(d+)
是一个捕获组 匹配一个或多个数字.
where (
and )
would match the literal parenthesis (needed to be escaped with a backslash because of the special meaning they have). (d+)
is a capturing group that would match one or more digits.
这篇关于Python - 正则表达式在括号之间获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - 正则表达式在括号之间获取数字


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01