MySelf
MySelf

Reputation: 31

I need regex replacement

I need regex replacement to emphasis fist letter of my movies list...

Sample:

I would like to get from this list

Camp (2004)
Can't Hardly Wait (1998)
Candy (2006)
Canyons, The (2014)
Daddy Day Care (2003)
Daddy and Them (2002)
Daddy's Home (2016)
Eating Out: All You Can Eat (2010)
Eddie the Eagle (2016)
Eddie (1997)

to get this

Camp (2004)
Can't Hardly Wait (1998)
Candy (2006)
Canyons, The (2014)

D

Daddy Day Care (2003)
Daddy and Them (2002)
Daddy's Home (2016)

E

Eating Out: All You Can Eat (2010)
Eddie the Eagle (2016)
Eddie (1997)

I tried something like this

^(\w).+\r\n^[^$1].+

to find two lines that start with different letter (which is main issue), but it doesn't work.

Can anyone help?

Upvotes: 1

Views: 79

Answers (3)

MySelf
MySelf

Reputation: 31

Thanks to all, but I see another issue right now, it make difference in capital letters and small letters and it wrongly break list and insert that letter.

Example:

...
...
I

Icon
IP Man

i

iPad

I

Iron Man
Ireland
....
...

but OK I will fix it later manually.

I prefer solution made by Wiktor Stribiżew

^(\w)(.*)(\R)(?!\1)(?=\w) replace with $1$2$3$3$1$3$3

but it needs to be tweaked to right letter precede movies that begins with that letter according to my edited question.

Thanks to all!!

Upvotes: 0

flaviodesousa
flaviodesousa

Reputation: 7845

I was able to achieve that using Sublime Text.

You could try:

^(.)(.*)\n(?!\1)

Replacing to:

$1$2\n\n$1\n\n

Edit (because the question was edited: letter prefixing instead of postfixing):

Again with Sublime Text, I did:

^(.)(.*)\n(?!\1)(.)

Replaced by:

$1$2\n\n$3\n\n$3

Upvotes: 2

melpomene
melpomene

Reputation: 85827

Here's a search/replace command that works in vim:

:%s/^\(.\).*\n\%(\1.*\n\)*/\1\r\r&\r/g

Instead of trying to find a line that doesn't start with some letter, we just match as many lines as possible that do start with the same letter.

Upvotes: 2

Related Questions