简体   繁体   中英

Replace every nth '\n' in a string

I'm trying to replace a bunch of \\n from a string, with whitespace and '//', but not ALL \\n in the string.

Luckily for me, there's a pattern: Every fourth \\n should not be removed.

Example:

\n jack ryan
\n policeman and pony enthusiast
\n german 
\n john anderson <--- don't remove this \n
\n fisherman
\n swedish 
\n barack putin <--- don't remove this \n
\n french
\n programmer

Output:

jack ryan // policeman and pony enthusiast // german
john // fisherman // swedish
barack // french // programmer

I've looked for a simple reg ex solution, but I haven't found what I need.

I hope someone can help!

Your input looks a little funny to me, but this would accomplish what you're asking:

input = '\n jack ryan \n policeman and pony enthusiast \n german \n john anderson \n fisherman \n swedish \n barack putin \n french \n programmer'

# Split on '\n':
lines = input.splitlines()

# Remove the first empty line:
lines = lines[1:]

output = ''
step_size = 3
for i in range(0, len(lines), step_size):
    output += '//'.join(lines[i:i + step_size]) + '\n'

print output

Output:

 jack ryan // policeman and pony enthusiast // german
 john anderson // fisherman // swedish
 barack putin // french // programmer

You can do this with a regular expression by capturing groups followed by newlines:

import re
re.sub(r"([^\n]+)\n([^\n]+)\n([^\n]+)(\n?)", r"\1 // \2 // \3\4", s) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM