这是一句“建筑物高100米,宽20米”我想提取高度为100的数字,所以我用
question = input " "
height = re.findall(r'(\d+) m tall', question)
然而,有时句子不是“100米高”,而是“100米高”。在这种情况下,我的程序无法再提取我想要的数字。有没有办法改进我的程序,让它工作无论句子包括“高”还是“高”。
答案 0 :(得分:4)
您可以查看&#34; tall或high&#34;条件来自<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<li ng-repeat="x in statistics">
<span>{{x.first}} :</span>
<span class="bold">{{x.second}}</span>
</li>
</body>
</html>
:
attr_accessible
演示:
|
如果您想要删除该字词,请使用非捕获组:
(\d+) m (tall|high)
答案 1 :(得分:1)
>>> import re
>>> re.findall(r'(\d+) m (?:tall|high)', "a building is 100 m tall and 20 m wide")
['100']
>>> re.findall(r'(\d+) m (?:tall|high)', "a building is 100 m high and 20 m wide")
['100']
答案 2 :(得分:0)
根据您的要求,正则表达式应匹配任何术语“tall”或“high”。
i.e., ?:tall|high
where, ?: means 'matches any of'
and, | means 'or'
所以,解决方案可以是:
>>> re.findall(r'(\d+) m (?:tall|high)', question)
['100']