Situation:
- text: a string
- R: a regex that matches part of the string. This might be expensive to calculate.
I want to both delete the R-matches from the text, and see what they actually contain. Currently, I do this like:
import re ab_re = re.compile('[ab]') text='abcdedfe falijbijie bbbb laifsjelifjl' ab_re.findall(text) # ['a', 'b', 'a', 'b', 'b', 'b', 'b', 'b', 'a'] ab_re.sub('',text) # 'cdedfe flijijie lifsjelifjl'
This runs the regex twice, near as I can tell. Is there a technique to do it all on pass, perhaps using re.split? It seems like with split based solutions I’d need to do the regex at least twice as well.
Outputs: