regex: if a line doesn't start with a particular string, add it as prefix

Geltrude picture Geltrude · Aug 11, 2012 · Viewed 7k times · Source

I've this text:

£££££Ciao a tutti§§§§§,§§§§§2009§§§§§,§§§§§8§§§§§
£££££Il mio nome è Geltry§§§§§,§§§§§2009§§§§§,§§§§§6§§§§§
Bla bla bla§§§§§,§§§§§2010§§§§§,§§§§§7§§§§§
£££££Le amiche della sposa§§§§§,§§§§§2011§§§§§,§§§§§3§§§§§

With TextCrawler I find all the lines that have not £££££ as a prefix, so:

^((?!£££££).)*$

What I should write as replacement?

Reg Ex: ^((?!£££££).)*$
Replacement: ?  <-- what write here?

to have:

£££££Ciao a tutti§§§§§,§§§§§2009§§§§§,§§§§§8§§§§§
£££££Il mio nome è Geltry§§§§§,§§§§§2009§§§§§,§§§§§6§§§§§
£££££Bla bla bla§§§§§,§§§§§2010§§§§§,§§§§§7§§§§§
£££££Le amiche della sposa§§§§§,§§§§§2011§§§§§,§§§§§3§§§§§

Answer

Alan Moore picture Alan Moore · Aug 11, 2012

Search:

^(?!£££££)(.+)$

Replace:

£££££$1

First, your regex was incorrect. ^((?!£££££).)*$ matches a line that doesn't contain £££££ anywhere. It also captures only the last character of the line, where you want it to capture the whole line.

My regex matches only lines that don't start with £££££. It captures the whole line in group number one, and (according to the manual), the $1 in the replacement string reinserts the captured text.

I also changed the * to + so it won't match empty lines and replace them with £££££.