Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197871

re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work?

From Veek M <veekjunk@foobar.com>
Newsgroups comp.lang.python
Subject re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work?
Date 2026-08-09 00:21 +0000
Organization A noiseless patient Spider
Message-ID <1158h6s$1ts3e$1@dont-email.me> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work? Veek M <veekjunk@foobar.com> - 2026-08-09 00:21 +0000
  Re: re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work? Jon Ribbens <jon+usenet@unequivocal.eu> - 2026-08-09 01:26 +0000
  Re: re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-08-09 11:24 +0000
    Re: re.sub(r'((?<=\A)|(?<=,))(?=,|\Z)', 'NA', ',1,,,two,3,,,') how does it work? Veek M <veekjunk@foobar.com> - 2026-08-11 09:21 +0000

csiph-web