Simple regex question (vim search/replace)

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).

Answers


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.


Need Your Help

Insert picture to gallery, name wrong

android image insert gallery

I want to save a picture into the gallery. it works with this code below, but the name from the picture is the System.currentTimeMillis .jpg

Lightweight workflow engine for Java

java workflow jbpm activiti

Is it better to write a new workflow engine use an existing BPM engine: jBPM 5, Activiti 5?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.