Re: replace number with zero-packed number
> I've never used \=, but I see it only works at the > beginning of the substitute string and makes the entire > replacement an expression. Is there a way to put it in the > middle. > > For example, I was thinking if the CSV file had other > numbers, and only one field should be 0 padded, ex: > > Blah, 745, blah, 98, blah > Blah, 34, blah, 200, blah > > And only the 4th field should be zero padded, you'd want > something like this: > > %s/\(\([^,]\+,\s*\)\{4\}\)\(\d\{1,6\}/\1\=someExprWith\2/g > > That's an eyesore, maybe not the best regex, but it'd work > if you could put the expr in the middle of the replace > string. Is this doable? There are a couple ways around this. You can reference those tagged items as you describe: %s/\(...\)regexp\(...\)/\=submatch(1).someExpr.submatch(2)/g using the submatch() function to call in the parts you referenced (in the expression, submatch(1) is the "\1" you'd refer to) Another idea would be to employ the "\zs" and "\ze" markers to specify where the beginning/end of the match are supposed to be replaced: %s/\(...\)\zsregexp\ze\(...\)/\=someExpr/g This requires the context in the \(...\), but the replaced match only spans from \zs through \ze which makes it somewhat easier to read. In your original regexp, there were an imbalance of \(...\) tagging, so I'm not sure what exactly you were attempting for, but it will choke unless you fix that. You also have nested \(...\) groups which are ambiguous as to which becomes which number in a replacement. A clearer idea would be to use \%(...\) which is a non-tagging (it doesn't get a \9 backreference number assigned to it) so you could do something like %s/^\(\%([^,]\+,\s*\)\{4}\)\(d\{1,6}\)/\=submatch(1).someExpr(submatch(2)) which would be similar to %s/^\(\%([^,]\+,\s*\)\zs\{4}\)\(d\{1,6}\)/\=someExpr(submatch(2)) You can read up on these at :help submatch() :help /\zs and before long, you'll be abusing them too. :) Oh, and if you want the 4th field, your \{...} should capture 3 fields, not 4, or otherwise you'll change the 5th (not 4th) field. -tim
RE: replace number with zero-packed number
Could I ask a follow-up question along these lines? I've never used \=, but I see it only works at the beginning of the substitute string and makes the entire replacement an expression. Is there a way to put it in the middle. For example, I was thinking if the CSV file had other numbers, and only one field should be 0 padded, ex: Blah, 745, blah, 98, blah Blah, 34, blah, 200, blah And only the 4th field should be zero padded, you'd want something like this: %s/\(\([^,]\+,\s*\)\{4\}\)\(\d\{1,6\}/\1\=someExprWith\2/g That's an eyesore, maybe not the best regex, but it'd work if you could put the expr in the middle of the replace string. Is this doable? *tim* -Original Message- From: A.J.Mechelynck [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 09, 2007 4:15 AM To: Luke Vanderfluit Cc: vim@vim.org Subject: Re: replace number with zero-packed number Luke Vanderfluit wrote: > Hi. I have a csv file that contains a field with a number, say 98. > I need to zerofill the fields to be 6 digits. > So any field that is say 98, should become 98. > > Is there an easy way to do this with search and replace? > > Thanks. > Kind regards. > There are several ways to do it, depending in part on what exactly you want to do. I haven't tested the following but I think they will work: - To fill in only the number under the cursor: no search-replace needed, place the Insert-mode cursor just before it and type in four zeros. - To replace the number 98 (only) wherever it appears as a word (thus 98 but not 498 or 987 or 89 or 0098): :%s/\<98\>/98/g - To replace the number 98 wherever it appears between non-digits (thus also k98g or \x98 which the above wouldn't replace, but still not 0098 or x098): :%s/\%(^\|[^[:digit:]]\)98\_[^[:digit:]]/98/g - To fill-in all numbers to at least 6 digits: :%s/\<\d\{1,5}/=("0" . submatch(0))[-6:]/g - To fill or truncate all numbers to exactly 6 digits (losing the millions if there are any): :%s/\<\d\+\>/=("0" . submatch(0))[-6:]/g All this, assuming that existing numbers have no thousands separators and that you don't want to add any. See :help :s :help /\< :help /\> :help /\d :help /multi :help [:digit:] :help sub-replace-expression :help submatch() :help expr-[:] etc. Best regards, Tony. -- Fortune's Real-Life Courtroom Quote #29: THE JUDGE: Now, as we begin, I must ask you to banish all present information and prejudice from your minds, if you have any ...
Re: replace number with zero-packed number
Luke Vanderfluit wrote: Hi. I have a csv file that contains a field with a number, say 98. I need to zerofill the fields to be 6 digits. So any field that is say 98, should become 98. Is there an easy way to do this with search and replace? Thanks. Kind regards. There are several ways to do it, depending in part on what exactly you want to do. I haven't tested the following but I think they will work: - To fill in only the number under the cursor: no search-replace needed, place the Insert-mode cursor just before it and type in four zeros. - To replace the number 98 (only) wherever it appears as a word (thus 98 but not 498 or 987 or 89 or 0098): :%s/\<98\>/98/g - To replace the number 98 wherever it appears between non-digits (thus also k98g or \x98 which the above wouldn't replace, but still not 0098 or x098): :%s/\%(^\|[^[:digit:]]\)98\_[^[:digit:]]/98/g - To fill-in all numbers to at least 6 digits: :%s/\<\d\{1,5}/=("0" . submatch(0))[-6:]/g - To fill or truncate all numbers to exactly 6 digits (losing the millions if there are any): :%s/\<\d\+\>/=("0" . submatch(0))[-6:]/g All this, assuming that existing numbers have no thousands separators and that you don't want to add any. See :help :s :help /\< :help /\> :help /\d :help /multi :help [:digit:] :help sub-replace-expression :help submatch() :help expr-[:] etc. Best regards, Tony. -- Fortune's Real-Life Courtroom Quote #29: THE JUDGE: Now, as we begin, I must ask you to banish all present information and prejudice from your minds, if you have any ...
Odp: replace number with zero-packed number
Dnia 9-05-2007 o godz. 7:19 Luke Vanderfluit napisał(a): > Hi. I have a csv file that contains a field with a number, say 98. > I need to zerofill the fields to be 6 digits. > So any field that is say 98, should become 98. > > Is there an easy way to do this with search and replace? Assuming you get your number: s/98/\=repeat('0', 6-len(submatch(0))).submatch(0)/ m. Morderstwa i krew ofiar na ścianach. Mroczne miasto pogrążone w chaosie. Sprawdź kto jest Przybywającym! Przybywający - thriller metafizyczny już w księgarniach. http://klik.wp.pl/?adr=http%3A%2F%2Fksiazki.wp.pl%2Fkatalog%2Fksiazki%2Fksiazka.html%3Fkw%3D10414&sid=1128
replace number with zero-packed number
Hi. I have a csv file that contains a field with a number, say 98. I need to zerofill the fields to be 6 digits. So any field that is say 98, should become 98. Is there an easy way to do this with search and replace? Thanks. Kind regards. -- Luke Vanderfluit Analyst / Web Programmer e3Learning.com.au 08 8221 6422