re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work?
Veek M <[email protected]>
| Newsgroups | comp.lang.python |
|---|---|
| Organization | A noiseless patient Spider |
| Message-ID | <[email protected]> |
look-ahead look-behind don't consume string so how does it advance through the string - could someone clearly explain how it works. re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') 'NA,1,NA,NA,two,3,NA,NA,NA' re.sub(r'(?<![^,])(?![^,])', 'NA', ',1,,,two,3,,,') 'NA,1,NA,NA,two,3,NA,NA,NA' My understanding is that there has to be a pattern that consumes the string eg: here [^,] consumes two 3 four and 5 but the look-ahead look- behind eliminate 5 re.findall(r'(?<=,)[^,]+(?=,)', '1,two,3,four,5') ['two', '3', 'four'] If it's matching the empty string '' then why don't we get NA,NA1 etc for ,1