Thursday, May 14, 2020

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

On Thu, May 14, 2020 at 08:00:38AM -0500, Tim Chase wrote:
>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.

Thank you, this works. I had to look up some of the flags you used before
I understood what you are doing. In my particular example I found I did
not need to use "\+", "\<" and "\>" to get the desired result. But I suspect that you are using
these to protect against scenarios I am not considering. Would you mind
elaborating on why you included those additional flags?

As I am trying to understand the "why"s of all of this, it would be helpful
for my understanding if someone would explain why my regex did not work as
expected. I am still not seeing it.

--
Wishing you only the best,

boB Stepp

--
--
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/20200514172709.GA17993%40Dream-Machine1.

No comments: