GolovDanil GolovDanil - 10 days ago 5
Python Question

regex matching complete expression containing multiple, similar terms

I have a string. For example:

s = "caption = #1081#1082#1083#1084', '#1087#1088 efiuefiu #23 3456"


I need to find all expressions consists of # and 4 digits, but I need to find expression
#1081#1082#1083#1084
, not
#1081
,
#1082
,
#1083
and
#1084
separately like this.

This doesn't work; it matches the individual expressions:

result = re.findall(r'#\d{4}', s)

Answer

You can look for one or more occurrences of # followed by 4 digits:

In [1]: import re

In [2]: s = "caption = #1081#1082#1083#1084', '#1087#1088 efiuefiu #23   3456"

In [3]: re.findall(r'(?:#\d{4})+', s)   
Out[3]: ['#1081#1082#1083#1084', '#1087#1088'] 
Comments