patternpythonMinor
Capturing the positions of "start" and "end" markers in a multi-line string
Viewed 0 times
capturingthepositionsmultilinemarkersstartandendstring
Problem
Is there any better (and shorter) way to get index of a regex match in Python?
For me, there is only one
import re
sstring = """
this is dummy text
which starts with nothing
and ends with something
"""
starts = re.finditer('start[s]?', sstring)
ends = re.finditer('end[s]?', sstring)
for m in starts:
print (m.start())
for m in ends:
print (m.end())For me, there is only one
starts and endsmatch in the string.Solution
If you are certain that there is exactly one match, then you don't need to iterate. You could write:
To note:
The spacing in
start, end = re.search('start.*ends?', sstring, re.DOTALL).span()To note:
- Take advantage of
re.DOTALLso that the regex can span multiple lines.
- Use
match.span()and destructuring assignment to get both thestartandendin one statement.
- The
[s]?afterstartis, from a mechanical viewpoint, useless. You might want to keep it just for symmetry.
The spacing in
print (something) is kind of weird; print(something) would be more conventional.Code Snippets
start, end = re.search('start.*ends?', sstring, re.DOTALL).span()Context
StackExchange Code Review Q#136527, answer score: 2
Revisions (0)
No revisions yet.