您可以在正则表达式上拆分字符串,然后循环遍历数据:
teststr = "First Line.............................234" \
"1.1.0 (L1) TestLine.........................567" \
"1.1.1 (L1) Second Line.............................587"\
"Third Line.............................856" \
"1.1.2 (L2) Fourth Line.............................775"\
"1.2.7 (L1) Fifth Line.............................262" \
"1.5.3 (L1) Sixth Line .............................346"\
"Seventh Line..............................234"
import re
results = re.split(r'(\(L\d+\))',teststr)
这会将输入拆分为类似于(Ln) 的任何值,其中n 可以是任何数字。
它给出了一个包含以下值的列表:
['First Line.............................2341.1.0 ',
'(L1)',
' TestLine.........................5671.1.1 ',
'(L1)',
' Second Line.............................587Third Line.............................8561.1.2 ',
'(L2)',
' Fourth Line.............................7751.2.7 ',
'(L1)',
' Fifth Line.............................2621.5.3 ',
'(L1)',
' Sixth Line .............................346Seventh Line..............................234']
在这种情况下,我们只想选择(L1) 之后的值,因此我们在列表上循环(滑动)并仅在它出现在(L1) 之后才打印该值。
for x, y in zip(results, results[1:]):
if x == '(L1)':
print(y)
完整的代码变成:
teststr = "First Line.............................234" \
"1.1.0 (L1) TestLine.........................567" \
"1.1.1 (L1) Second Line.............................587"\
"Third Line.............................856" \
"1.1.2 (L2) Fourth Line.............................775"\
"1.2.7 (L1) Fifth Line.............................262" \
"1.5.3 (L1) Sixth Line .............................346"\
"Seventh Line..............................234"
import re
results = re.split(r'(\(L\d+\))',teststr)
for x, y in zip(results, results[1:]):
if x == '(L1)':
print(y)
这给出了:
TestLine.........................5671.1.1
Second Line.............................587Third Line.............................8561.1.2
Fifth Line.............................2621.5.3
Sixth Line .............................346Seventh Line..............................234