Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
在python3正则表达式中省略可选单词的问题_Python_Regex_Python 3.x - Fatal编程技术网

在python3正则表达式中省略可选单词的问题

在python3正则表达式中省略可选单词的问题,python,regex,python-3.x,Python,Regex,Python 3.x,我需要一个正则表达式,它可以捕获两个组:一部电影和一年。或者,它们之间可能有一个“from”字符串 我的预期结果是: first_query="matrix 2013" => ('matrix', '2013') second_query="matrix from 2013" => ('matrix', '2013') third_query="matrix" => ('matrix', None) 我已经为python3做了两次模拟: I-r“(.+)(?:from){0,

我需要一个正则表达式,它可以捕获两个组:一部电影和一年。或者,它们之间可能有一个“from”字符串

我的预期结果是:

first_query="matrix 2013" => ('matrix', '2013')
second_query="matrix from 2013" => ('matrix', '2013')
third_query="matrix" => ('matrix', None)
我已经为python3做了两次模拟: I-
r“(.+)(?:from){0,1}([1-2]\d{3})”
不匹配第一个查询和第三个查询,也不省略第一组中的“from”,这是我想要避免的

II-
r“(.+)(?:from){1}([1-2]\d{3})”
可用于第二个\u查询,但与第一个\u查询和第三个\u查询不匹配

是否可以匹配所有三个字符串,从第一组中省略“from”字符串

提前感谢。

您可以使用

^(.+?)(?:\s+(?:from\s+)?([12]\d{3}))?$

详细信息

  • ^
    -字符串的开头
  • (.+?)
    -第1组:除换行符以外的任何1+字符,尽可能少
  • (?:\s+(?:from\s+)([12]\d{3}))?
    -一个可选的非捕获组,与下列事件的1或0次匹配:
    • \s+
      -1+空格
    • (?:from\s+)
      -可选的
      from
      子字符串序列,后跟1+空格
    • ([12]\d{3})
      -第2组:
      1
      2
      ,后跟3位数字
  • $
    -字符串结束
您可以使用

^(.+?)(?:\s+(?:from\s+)?([12]\d{3}))?$

详细信息

  • ^
    -字符串的开头
  • (.+?)
    -第1组:除换行符以外的任何1+字符,尽可能少
  • (?:\s+(?:from\s+)([12]\d{3}))?
    -一个可选的非捕获组,与下列事件的1或0次匹配:
    • \s+
      -1+空格
    • (?:from\s+)
      -可选的
      from
      子字符串序列,后跟1+空格
    • ([12]\d{3})
      -第2组:
      1
      2
      ,后跟3位数字
  • $
    -字符串结束

这将输出您的图案,但数字中的空格太多:

import re

pat = r"^(.+?)(?: from)? ?(\d+)?$"


text = """matrix 2013
matrix from 2013
matrix"""

for t in text.split("\n"):
    print(re.findall(pat,t))
输出:

[('matrix', '2013')]
[('matrix', '2013')]
[('matrix', '')]
说明:

 ^           start of string
(.+?)        lazy anythings as few as possible
(?: from)?   non-grouped optional ` from`
 ?           optional space
(\d+=)?$     optional digits till end of string


演示:

这将输出您的图案,但在数字的开头有太多的空间:

import re

pat = r"^(.+?)(?: from)? ?(\d+)?$"


text = """matrix 2013
matrix from 2013
matrix"""

for t in text.split("\n"):
    print(re.findall(pat,t))
输出:

[('matrix', '2013')]
[('matrix', '2013')]
[('matrix', '')]
说明:

 ^           start of string
(.+?)        lazy anythings as few as possible
(?: from)?   non-grouped optional ` from`
 ?           optional space
(\d+=)?$     optional digits till end of string

演示:

重新导入
模式=重新编译(r“”
^\s*#字符串开头(可选空格)
(?P\S+)#一个或多个非空白字符(标题)
(?:\s+from)?#可选地,在一些空格后加上单词“from”
\s*#可选空白
(?P[0-9]+)?#可选数字字符串(年份)
\s*$#字符串结尾(可选空格)
“”,re.VERBOSE)
对于[‘矩阵2013’、‘矩阵自2013’、‘矩阵’中的查询:
m=重新匹配(模式、查询)
如果m:print(m.groupdict())
#印刷品:
#{'title':'matrix','year':'2013'}
#{'title':'matrix','year':'2013'}
#{'title':'matrix','year':无}
免责声明:此正则表达式不包含拒绝前两个匹配项所需的逻辑,理由是矩阵实际上是在1999年出现的。

import re
模式=重新编译(r“”
^\s*#字符串开头(可选空格)
(?P\S+)#一个或多个非空白字符(标题)
(?:\s+from)?#可选地,在一些空格后加上单词“from”
\s*#可选空白
(?P[0-9]+)?#可选数字字符串(年份)
\s*$#字符串结尾(可选空格)
“”,re.VERBOSE)
对于[‘矩阵2013’、‘矩阵自2013’、‘矩阵’中的查询:
m=重新匹配(模式、查询)
如果m:print(m.groupdict())
#印刷品:
#{'title':'matrix','year':'2013'}
#{'title':'matrix','year':'2013'}
#{'title':'matrix','year':无}

免责声明:此正则表达式不包含拒绝前两个匹配项所需的逻辑,理由是矩阵实际上是在1999年出现的。

感谢您的澄清!谢谢你的澄清!这也是一个很好的解决方案!这是一个很好的方法,通过将正则表达式代码拆分成行来记录它。也是一个很好的解决方案!通过将正则表达式代码分成几行来记录它是一种很好的方法。