这是我的另一个问题:
string = "Organization: S.P. Dyer Computer Consulting, Cambridge MA"
尽管使用正则表达式在“ Organization:”之后出现了句号,数字或其他字符,我该如何提取所有字符?
result_organization = re.search("(Organization: )(\w*\.*\w*\.*\w*\s*\w*\s*\w*\s*)", string)
我上面的代码很长,一点也不明智。
答案 0 :(得分:1)
您不需要正则表达式,这个简单的代码应该可以为您提供所需的结果:
str = "Organization: S.P. Dyer Computer Consulting, Cambridge MA";
if str.startswith("Organization: "):
str = str[14:];
print(str)
您还可以使用模式(?<=Organization: ).+
说明:
(?<=Organization: )
-向后看,断言前面是否是Organization:
.+
-匹配换行符以外的任何字符。
答案 1 :(得分:1)
我建议使用这种查找命令
print(string[string.find("Organization")+14:])
答案 2 :(得分:0)
您可以使用一个捕获组,而不是2个捕获组。
除了指定所有单词 public function asyncReports()
{
$dados = $Request;
}
之外,您可以选择使用点匹配除换行符以外的任何字符,然后匹配0+次以匹配到结尾。
但是请注意,这也将匹配(\w*\.*\w*\.*\w*\s*\w*\s*\w*\s*)
#@$$ ++
例如
^Organization: (.+)
如果您想要更具限制性的模式,可以使用character class并指定允许匹配的内容。例如:
import re
string = "Organization: S.P. Dyer Computer Consulting, Cambridge MA"
result_organization = re.search("Organization: (.*)", string)
print(result_organization.group(1))