0

I don't know enough about regex to come up with a smarter way to turn a list of addresses from blocks into single lines.

Here's an example:

@sdfqsdf
qsdfqdsf
USA

@sdfqsdf
qsdfqdsf
USA
etc.

I currently use the brain-dead regex:

SEARCH ^@(.+?)\r\n(.+?)\r\n(.+?)\r\n(.+?)\r\n\r\n
REPLACE \1, \2, \3, \4\r\n

It's not very good because it assumes each address has four lines, and besides, it's kludgy.

I guess a smarter solution would be to tell the Perl (Boost) regex engine that a block ends with two CRLF's and that each line in between that ends with a single CRLF should have it stripped and replace with a comma, but I have no idea how to do this.

Has someone done this before and could tell me?

Thank you.

Gulbahar
  • 5,343
  • 20
  • 70
  • 93

1 Answers1

0

Iterate over all lines and replace every line ending on an empty line with a comma and a space.

The following regex should work for a line wise approach (search and replace with ", ")

(?<=.+)\r\n(?!\r\n)

Adapt \r\n to your system's newline character.

If you want to do it in a text editor, the VIM command is

:%s/.\+\zs\r\n\ze\r\n\@!/, 

Remember to adapt \r\n and do not forget the space character after the ,

Lucas Hoepner
  • 1,437
  • 1
  • 16
  • 21
  • Thanks. That's what I thought of doing if it couldn't be done by just providing a regex in a text editor. – Gulbahar Jan 16 '13 at 14:37