kyoob
kyoob

Reputation: 519

How do I use regex to find long lines and insert a newline at the last comma before a given column?

I have a text file open in Notepad++ in which some lines go past 112 columns, which I'd like to avoid. By the time any of these lines gets to the 112th column, a comma has appeared in the string. Like this.

1,2,3,4...109,110,111,112,113

(Let's suspend disbelief and imagine three-digit numbers take up one column each)

In the end I'd like something like this:

1,2,3,4...109,110,111,112,
113

So far I've figured out the regular expression to find all the lines that are too long:

^.{113,}$

For the life of me I can't figure how to capture the comma I'm looking for in the string up to that column so I can sub in a newline after it.

Anyone know how this can be done?

Upvotes: 1

Views: 1000

Answers (2)

Bergi
Bergi

Reputation: 664936

This should do it:

^(?=.{112})(.{0,111},)

It matches the begin of a line with at least 112 characters (by lookahead), then matches as many characters as possible (up to 111) before a comma.

Replace this with with the captured group followed by a linebreak (\1\n).

Upvotes: 3

David
David

Reputation: 6561

Use parentheses to create a capture group:

^(.{112},).+

This will identify lines greater than 112 columns. Then take the capture group, append a newline, and replace the capture group in the line.

Upvotes: 0

Related Questions