0

I have multiple numbers in lines of 16, for example

  $02 c, $3f c, $fe c, $01 c, $ff c, $06 c, $00 c, $f8 c, $06 c, $1f c, $fe c, $00 c, $7f c, $8c c, $00 c, $fc c,

with pattern [$][0-9][0-9]\s@, and I need to substitute those lines by grouping the numbers in set of four and adding group after each group, like

 $02 $3f $fe $01 group $ff $06 ...

Is there a short way to do this, or I have to copy and paste same pattern four times in the :s command ?

Edit: the pattern should be: \$\x\x c,, thanks DJMcMayhem

1

Your regex doesn't seem right. [$][0-9][0-9]\s@, won't match the hex digits (such as 3f), and it seems like there's a c after each number, not an @. Don't you mean they match this pattern?

[$][0-9a-f][0-9a-f]\sc, 

or even simpler

\$\x\x c, 

But anyway, on to your main question. Yes, you can repeat a pattern without having to type it out multiple times. For example:

\(foo\)\{3}

will match "foo" repeated 3 times. I'd recommend reading through quantifiers on vimregex.com.

In your specific example, I'd recommend two regexes. One to remove the "c"s:

:s/ c,//g

Followed by a regex to add the "group" text:

:s/\v(\$\x\x ){4}/&group /g
  • 1
    For the first regex, I would recommend %s/ c, \?/ /g. That way you get a trailing space on the line, that is needed by the second regex. From the question, I assume that a trailing "group" is needed. – Ralf Mar 5 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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