Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 如何创建匹配的正则表达式`change://problem/59689726`及`change://1234567`_Python_Regex - Fatal编程技术网

Python 如何创建匹配的正则表达式`change://problem/59689726`及`change://1234567`

Python 如何创建匹配的正则表达式`change://problem/59689726`及`change://1234567`,python,regex,Python,Regex,我正在尝试正则表达式匹配字符串change://problem/59689726和change://1234567但是下面的代码只匹配前者,我如何更改正则表达式以匹配两者?如何使问题/可选 输入代码: import re out = ''' <change://problem/59689726> This is a test1 change://1234567 This is a test2 [Problem] This is problem desription ''' m = r

我正在尝试正则表达式匹配字符串
change://problem/59689726
change://1234567
但是下面的代码只匹配前者,我如何更改正则表达式以匹配两者?如何使
问题/
可选

输入代码:

import re
out = '''
<change://problem/59689726> This is a test1
change://1234567 This is a test2
[Problem]
This is problem desription
'''
m = re.findall("[\S]*(?:change:\/\/problem\/(\d{8,8}))", out.split('[Problem]')[0])
if m:
    for radar in set(m):
        print radar

CURRENT OUTPUT:-
59689726
1234567如果您需要在
change://problem/
更改://
您可以使用

re.findall("change://(?:problem/)?(\d+)", out.split('[Problem]')[0])
请参阅和

模式匹配:

  • change://
    -文本字符串
  • (?:problem/)?
    -1或0次出现
    问题/
    子字符串
  • (\d+)
    -第1组(由
    re.findall
    返回的内容):1+位数

    • 1234567
      是7位数字。数字长度未知吗?我在哪里可以理解/阅读
      (?:problem/)?
      ?我们为什么需要
      ?:
      @user3682248和。
      re.findall("change://(?:problem/)?(\d+)", out.split('[Problem]')[0])