Reputation: 79635
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).
Upvotes: 0
Views: 173
Reputation: 117959
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.
Upvotes: 5