1

I have similar text on more lines:

_AB_ _ABC_ _ OR _ _A_ _ABC_

I need to replace only the underscores that delimit some text. So I need:

=AB= =ABC= _ OR _ =A= =ABC=

How to match each pattern?

4

Try:

%s/_\([A-Za-z]\+\)_/=\1=/g

Where

  • [A-Za-z] matches the chars A-Z and a-z. You could add other chars between []. Note: If you want to add a dash (-), it has to be the first char after the opening [.
  • \+ means "one or more".
  • Note that you can replace [A-Za-z] with \a (or you could use \w to add [0-9]` too). See :h /character-classes – statox Feb 7 at 12:38
  • 2
    @statox I knew \w, but that would also add the underscore. But \a is good (and I didn't knew it :-)). – Ralf Feb 7 at 12:44
  • If you're looking for single-token representations of those don't forget the POSIX style character classes [:alpha:] and [:alnum:] e.g. /[[:alnum:]]. (Or do forget them...they're not particularly short and they're ASCII only. :) – B Layer Feb 8 at 12:25

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.