0

How do I specify that there are several options for a string in a search? For example, I want to find any combination that start with either jspPar, btn or jspAtt that ends with the letter K.

Also - I need to replace it with a string depending on the original prefix. for example, if the prefix was jspPar I need to replace it with the letter P. (and, let's say, B and A for btn and jspAtt accordingaly).

1 Answer 1

5

Is

\(jsPar\|btn\|jspAtt\)[^ \t]*K

what you are looking for?

The \(jsPar\|btn\|jspAtt\) says “at this point, match any of these alternatives”, then [^ \t]* says “at this point, match any amount (incl. zero) of space or tab characters”, and K of course means “at this point match a K”.


For your added question could do something like this:

%s/\(jsPar\|btn\|jspAtt\)[^ \t]*\zsK/\=submatch(1) == 'jsPar' ? 'P' : submatch(1) == 'btn' ? 'B' : 'A' /g

(The \zs says “consider the match to have started at this point” so only the “K” will be replaced.)

But I would only do that if I had to do the substitution in a single pass. Otherwise I’d just run three s///s:

%s/jspAtt[^ \t]*\zsK/A/g
%s/jsPar[^ \t]*\zsK/P/g
%s/btn[^ \t]*\zsK/B/g

Given command history, that’s much less typing, and is also very unlikely to require debugging, whereas that’s always a potentiality when specifying any computation.

Sign up to request clarification or add additional context in comments.

3 Comments

The ORs need to be escaped, I believe. \| See softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml
Yeah, already fixed. I can never remember which non-Perl regexp dialect requires me to escape them or not.
anything about my addition to the question?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.