Gautam
Gautam

Reputation: 375

What is wrong with this vim regular expression?

I have a list of files with extension .elf like this

file1.elf file2.elf file3.elf

I am trying to run them in shell with run command like run file1.elf >file1.log and get the result in a log file with file name with .log addition. My list of file is very big. I am trying out a vim regular expression so it will match the file name eg file1 in file1.elf and use it to create name for the log file. I am trying out like this

s/\(\(\<\w\+\)\@<=\.elf\)/\1 >\2\.log/

Here i try to match a text which is proceeded by .elf and keep it in \1 , i expect the entrire file name to be in it and \2 i was hoping would just contain the file name minus extension. but this gives me run file1 >file1.run i.e \1 dose not take the full file name, it has some how missed .elf extension. I can do \1\.elf to get proper result but i was wondering why the expression is not working as i expected?

Upvotes: 1

Views: 59

Answers (2)

Ren&#233; Nyffenegger
Ren&#233; Nyffenegger

Reputation: 40499

You use \@<= in your match pattern. This is the positiv lookahead assertion. As per documentation (:help /\@<=1),

Matches with zero width if the preceding atom matches just before what follows

The important part is that it matches with zero width, this is what you are experiancing, the .elf (which follows) is matched but with zero widht, so that \1 does not contain the suffix .elf.

Instead, it would be easier to go with a

%s/\v(.*)\.elf$/run \1.elf > \1.log/

Here, I've used \v to turn on very magic (:help magic). With this turned on, you don't need al those backslashes when you use grouping parantheses.

Then there is (.*) to match and store the filename up until

\.elf$ which seems to be each files suffix.

In the substitution part, after the / I add the literal run followed by \1. \1 will be replaced by the stored filename (without .elf suffix).

Upvotes: 2

FDinoff
FDinoff

Reputation: 31419

The \@<= seems pointless and unneeded. Removing it gets you the desired behavior.

Upvotes: 1

Related Questions