Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用正则表达式从字符串中获取特定的数字部分_Python_Regex - Fatal编程技术网

Python 如何使用正则表达式从字符串中获取特定的数字部分

Python 如何使用正则表达式从字符串中获取特定的数字部分,python,regex,Python,Regex,我需要将“16082219”放入一个变量中。请帮助我使用python中的正则表达式。我尝试了很多事情,但都没有成功。你的问题并不完全正确。你在找什么?8位数的序列?空格之间的数字?第二个字?行尾的第二个字?看看python3的几个答案,他们都会发现使用完全不同的正则表达式是一样的 result = ['\nR0 16082219 16.9(5r) '] 重新导入 结果=['\nR0 16082219 16

我需要将“16082219”放入一个变量中。请帮助我使用python中的正则表达式。我尝试了很多事情,但都没有成功。

你的问题并不完全正确。你在找什么?8位数的序列?空格之间的数字?第二个字?行尾的第二个字?看看python3的几个答案,他们都会发现使用完全不同的正则表达式是一样的

result = ['\nR0        16082219            16.9(5r)                            ']
重新导入
结果=['\nR0 16082219 16.9(5r)']
re_d8=re.compile(r'(?P\d{8}))
m_d8=re_d8.搜索(结果[0])#搜索八位数字
如果m_d8:
print(f“d8={m_d8.group('d8')}”)
re_2=re.compile(r'[^]++(?P\d+))#搜索第二个仅包含数字的单词
m_2=re_2.搜索(结果[0])
如果m_2:
打印(f“digits={m_2.group('digits')}”)
re_3=re.compile(r'(?P\d+[^]+$)#从末尾搜索第二个单词
m_3=re_3.搜索(结果[0])
如果m_3:
print(f“d3={m_3.group('d3')}”)
re_sDs=re.compile(r'(?P\d+))#在空格之间搜索数字
m_sds=重新搜索(结果[0])
如果m_sds:
打印(f“sds={m_sds.group('sds')}”)

您尝试过regex101.com吗?另外,请编辑您的答案以显示您尝试过的内容。
import re
result = ['\nR0 16082219 16.9(5r) ']
re_d8 = re.compile(r'(?P<d8>\d{8})')
m_d8 = re_d8.search(result[0])  # search for eight digits
if m_d8:
    print(f"d8={m_d8.group('d8')}")

re_2 = re.compile(r'[^ ]+ +(?P<digits>\d+) +')  # search for second word which contains only digits
m_2 = re_2.search(result[0])
if m_2:
    print(f"digits={m_2.group('digits')}")

re_3 = re.compile(r'(?P<d3>\d+) [^ ]+ $')  # search for the second word from the end
m_3 = re_3.search(result[0])
if m_3:
    print(f"d3={m_3.group('d3')}")

re_sDs = re.compile(r' (?P<sds>\d+) ')  # search for numbers between spaces
m_sds = re_sDs.search(result[0])
if m_sds:
    print(f"sds={m_sds.group('sds')}")