Reputation: 1593
I have a string 1 blahblahblah 2 sdsdsdsdsd 3 uuuuuu 4 eeee 5 abcdef
I would like to output
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
I have tried to add \n
before every number by using re.split
but it didn't work
re.split(' (?=[1-9]:)', line)
Upvotes: 1
Views: 4215
Reputation: 13410
Without using re
:
>>> s = '1 blahblahblah 2 sdsdsdsdsd 3 uuuuuu 4 eeee 5 abcdef'
>>> for i,word in zip(*[iter(s.split())]*2):
i = int(i)
print('{i} {word}'.format(**locals()))
Output:
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
Upvotes: 0
Reputation: 879291
Here is another way to do the substitution without inserting a newline before the first number:
In [62]: print(re.sub(r'\s(\d)', r'\n\1', line))
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
Upvotes: 1
Reputation: 1121486
The following works just fine:
re.sub(r'(\d+)', '\n\\1', input)
Demo:
>>> print(re.sub(r'(\d+)', r'\n\1', input))
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
The expression (\d+)
matches 1 or more digits, and I replace that with a newline followed by the matched digits (via the capturing group).
Upvotes: 5
Reputation: 287775
Use a group:
>>> s = '1 blahblahblah 2 sdsdsdsdsd 3 uuuuuu 4 eeee 5 abcdef'
>>> import re
>>> print(re.sub(r'([0-9]+)', r'\n\1', s))
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
To remove the first newline, you can add a negative lookbehind:
>>> print(re.sub(r'(?<!^)([0-9]+)', r'\n\1', s))
1 blahblahblah
2 sdsdsdsdsd
3 uuuuuu
4 eeee
5 abcdef
Upvotes: 1