例如,如果我有代码:
for row in self.reader:
if row:
if row[0] == "Stage * Disk Face":
"""CODE TO EXECUTE"""
但是,有没有办法让星号*
任意数字,以便在找到字符串"Stage 1 Disk Face"
或"Stage 2 Disk Face"
或"Stage 3 Disk Face"
等时,代码会执行吗?原因并不是每个阶段编号都有磁盘面功能。我基本上需要找出是否至少有一个,任意数字。
答案 0 :(得分:4)
这应该有效:
import re
for row in self.reader:
if row:
if re.match('^Stage [0-9]+ Disk Face$', row[0]):
"""CODE TO EXECUTE"""
答案 1 :(得分:0)
检查
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(Stage )[0-9]( Disk Face)"
test_str = "Stage 1 Disk Face"
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitutio
答案 2 :(得分:0)
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI"
答案 3 :(得分:0)
使用正则表达式最简单:
import re
rows = [["Stage 1 Disk Face"], ["Stage 2 Disk Face"], ["Stage 3 Disk Face"]]
pattern = re.compile("Stage .* Disk Face")
for row in rows:
if row:
if pattern.match(row[0]):
print(row[0])