1

Running Windows 10 (Home) and

VIM - Vi IMproved 8.1 (2018 May 18, compiled May 18 2018 18:36:07)
MS-Windows 32-bit GUI version with OLE support
Included patches: 1
Compiled by mool@tororo.

I have read both http://www.vimregex.com/ and various sections of http://vimdoc.sourceforge.net/htmldoc/help.html. That said, I just can't figure out what I'm doing incorrectly. Please follow the live example below:

Using \+ -or \* will not work for me in the following example

Original 01 --> Theft Attempted or Completed Offense? Completed     
Original 02 --> Theft Attempted or Completed Offense? Attempted

Because the words Completed and Attempted randomly alternate on various lines, I was hoping \u\w\+ would help. No matter what I try, once I employ \+ or \*, that line of code/script fails:

" Broken
:%s/\v\s(Attempted or Completed Offense\?)\s(\u\w\+)\s/,\1,\2,/g
" Working
:%s/\v\s(Attempted or Completed Offense\?)\s(\u\w.......)\s/,\1,\2,/g

I cannot find a written reason for this failure.

2
  • 3
    since you are using \v, use + instead of \+
    – Sundeep
    Commented Mar 9, 2022 at 6:24
  • 2
    More specifically you probably want to read :h /magic and :h pattern-overview and the following sections of pattern.txt
    – statox
    Commented Mar 9, 2022 at 10:29

1 Answer 1

2

According to :help \v,

                            */\v* */\V*
Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z',
'A'-'Z' and '_' have special meaning: "very magic"

"Special meaning" includes quantifiers. That means + has special meaning, and \ quotes it, preventing its special meaning.

Other characters have a special meaning without a backslash.  They need to be
preceded with a backslash to match literally.  For example "." matches any
character while "\." matches a dot.

So the following should work:

:%substitute/\v\s(Attempted or Completed Offense\?)\s(\u\w+)\s/,\1,\2,/g
4
  • Thank you 'all' for your time and feedback. Admittedly, I was working with and editing other commands that have worked on similar patterns to mine. I also do not (yet) fully understand the usage of "\v"; I intend to read that :help text as well as the other suggested :help files. I do struggle to understand and/or correctly interpret what a lot of the help files are trying to convey. For 'me' most of those provided examples are vague; so I end up frustrated. Once again thank you all.
    – query-01
    Commented Mar 10, 2022 at 3:42
  • @query-01 you’re welcome! Since you’re new here, I’ll point out that voting is how we express our judgement that an answer is of good quality, and accepting answers is how we indicate what solved the problem
    – D. Ben Knoble
    Commented Mar 10, 2022 at 13:22
  • I intend to do just that, however I need time to digest and fully understand this newly acquired knowledge --which will help me chose the best response that solved the concern. Frankly, every response contributed helped; in one way or another. New data to read, some previous data that I've read, but did not fully understand, or I misinterpreted its meaning. I have not had a chance to try the supplied code; was more interested in finding the correction to my error, by way of reading/rereading the proper and any additional help text.
    – query-01
    Commented Mar 11, 2022 at 6:55
  • 1
    @query-01 Please accept this answer. The community bot is bumping this question because it assumes current answers might not be satisfactory.
    – r_31415
    Commented Aug 6, 2022 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.