Search code examples
regexnotepad++regex-groupregex-greedy

How to repeat existing word with placeholder to each row in Notepad ++?


current lines are

cp filename1*.csv 
cp filename2*.csv 
cp filename3*.csv 
....
cp filenamen*.csv 

I'd like to replace these rows with extra words, eventually all lines should be like this

cp filename1*.csv filename1.csv 
cp filename2*.csv filename2.csv 
cp filename3*.csv filename3.csv 
....
cp filenamen*.csv filenamen.csv 

So it is like to replace

{cp\s}{filename\d+}{.csv}

with

{0}{1}{2}{1}

Solution

  • I was able to transform your stuff in Notepad++ from

    cp filename1*.csv 
    cp filename2*.csv 
    cp filename3*.csv 
    

    to

    cp filename1*.csv filename1.csv 
    cp filename2*.csv filename2.csv 
    cp filename3*.csv filename3.csv 
    

    With a finding regex of cp(\s+)(\w+)(\d+)\*\.csv and replace regex of cp\1\2\3*.csv \2\3.csv.

    I am not sure if the star was supposed to be in the same since it was not clear from your post whether it means something else or not, so I just assumed it was a literal character and escaped it with \*. If it's supposed to be something else like more numbers, you can trivially fix up the answer with your knowledge since you appear to be familiar with regex already. Also if the * is supposed to be multiple digits, then the \d+ will handle that.

    I also captured the spaces because you did that in your example, and I followed it by capturing and outputting to try to keep it as close to what you wanted as possible.