Thursday, May 14, 2020

Re: How to search a match on a line and then make a change on the next line?

On 2020-05-13 22:38, boB Stepp wrote:
> I am relatively new to using regex pattern matching techniques to
> search for and replace text. Today I have been trying to get a
> better mastery of these techniques. But I am currently stumped on
> the following text search and replace:
>
> # Home address fields:
> # field address STANDARD FIELD -- CANNOT EDIT!
> # field address2 STANDARD FIELD -- CANNOT EDIT!
> pobox = "PO Box", string


So based on my reading of your description & regex, you want this to
become

field pobox

rather than

# field pobox

(with the leading hash+space)?

> In the above I want to change the lines starting with "pobox" to
> match the format of the line above it, e.g., "field pobox", "field
> work_pobox" and "field other_pobox", respectively. My best effort
> so far to do this is:
>
> :g/\(field [[:alnum:]_]*\)address2/+s/pobox/\1pobox/

I'd go with something like

:%s/\(\<field \+\w*\)address2.*\n\s*\zs\zepobox\>/\1

which captures the "field" and optional prefix, then sets the start &
end of the match with \zs and \ze so that the replacement-capture
gets inserted there.

-tim




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200514080038.74e0b9aa%40bigbox.attlocal.net.

No comments: