> 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