3
\$\begingroup\$

Is there any better (and shorter) way to get index of a regex match in Python?

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.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

If you are certain that there is exactly one match, then you don't need to iterate. You could write:

start, end = re.search('start.*ends?', sstring, re.DOTALL).span()

To note:

  • Take advantage of re.DOTALL so that the regex can span multiple lines.
  • Use match.span() and destructuring assignment to get both the start and end in one statement.
  • The [s]? after start is, 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.

\$\endgroup\$
0
1
\$\begingroup\$

I am no python expert but I think if the pattern contains only one match group we can use re.search(pattern, string).start() method instead of iter object.

print(re.search('start[s]?', sstring).start())
print(re.search('end[s]?', sstring).end())
\$\endgroup\$
2
  • \$\begingroup\$ We expect an answer to contain an actual review. This is not a review. Please add an actual review to your answer or risk this answer getting removed. \$\endgroup\$
    – Mast
    Commented Aug 1, 2016 at 11:18
  • 2
    \$\begingroup\$ I am new to codereview. I will look at how to for this group. \$\endgroup\$ Commented Aug 1, 2016 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.