No, the match does not show the data that was cut off by itself.
The Match object that a regex gives you contains information about where data was found, you could extract it with that
import re
p = r'\d(?<=)'
s = '1a'
match = next(re.finditer(p, s))
# >>> match
# <re.Match object; span=(0, 1), match='1'>
head = match.string[:match.start()] # ""
tail = match.string[match.end():] # "a"
Note that re.findall
doesn't give you Match
-objects, you'll need another function that does that, like re.finditer
. I'm using next()
here because it returns an iterator instead of a list, you'd usually cast it to a list or loop over it.
Another option would be to make these groups in your pattern directly.
If you're interested in both, before and after the match:
import re
p = r'(^.*?)(\d)(.*$)'
s = '1a'
re.findall(p, s)
# [('', '1', 'a')]
But this will not give you multiple results results in the same string, as they are overlapping and you can't have variable-with lookbehinds in the builtin re
library.
If you're only interested in the string after the match, then you can do that
import re
p = r'(\d)(?=(.*))'
s = '1a'
re.findall(p, s)
# [('1', 'a')]
s = '1a2b'
re.findall(p, s)
# [('1', 'a2b'), ('2', 'b')]