Найти - Пользователи
Полная версия: Регулярное выражение - разбивка на массив
Начало » Python для новичков » Регулярное выражение - разбивка на массив
1
TITANius
Не могу придумать как разбить строку:

[13 28 22] Rebeca Storn normal armor buffer ishtar[13 28 24] Nianah expensive neuts[13 28 29] Nianah 77mil each[13 28 39] Rebeca Storn yes was about to say that[13 28 48] Captain Scarz ya but i cant use t2 medium neuts yet[13 28 49] RebecaStorn storyline neuts are too expensive[13 28 56] Rebeca Storn fit meta 4 then[13 29 14] Captain Scarz k[13 29 18] Nianah those 50W neuts
на массив

[13 28 22] Rebeca Storn normal armor buffer ishtar
[13 28 24] Nianah expensive neuts
[13 28 29] Nianah 77mil each
[13 28 39] Rebeca Storn yes was about to say that
[13 28 48] Captain Scarz ya but i cant use t2 medium neuts yet
[13 28 49] RebecaStorn storyline neuts are too expensive
[13 28 56] Rebeca Storn fit meta 4 then
[13 29 14] Captain Scarz k
[13 29 18] Nianah those 50W neuts

заранее благодарен
fata1ex
re.sub('(\[.+?\])', lambda sub: '\n' + sub.group(1), s)

Если нужен именно список:
re.sub('(\[.+?\])', lambda sub: '\n' + sub.group(1), s).split('\n')

Или так:
result = []
for first_split in s.split('['):
    second_split = first_split.split(']')
    if second_split[0]:
        result.append('[' + second_split[0] + ']' + second_split[1])
Ed
Или так:
['[' + l for l in s.split('[') if l]
или так:
re.sub('\[', '\\n[', s).split('\n')[1:]
fata1ex
Ed, ты победил :(
py.user.next
>>> import re
>>> 
>>> s = "[13 28 22] Rebeca Storn normal armor buffer ishtar[13 28 24] Nianah expensive neuts[13 28 29] Nianah 77mil each[13 28 39] Rebeca Storn yes was about to say that[13 28 48] Captain Scarz ya but i cant use t2 medium neuts yet[13 28 49] RebecaStorn storyline neuts are too expensive[13 28 56] Rebeca Storn fit meta 4 then[13 29 14] Captain Scarz k[13 29 18] Nianah those 50W neuts"
>>> 
>>> lst = re.findall(r'\[\d+ \d+ \d+\][^[]+', s)
>>> 
>>> print(*lst, sep='\n')
[13 28 22] Rebeca Storn normal armor buffer ishtar
[13 28 24] Nianah expensive neuts
[13 28 29] Nianah 77mil each
[13 28 39] Rebeca Storn yes was about to say that
[13 28 48] Captain Scarz ya but i cant use t2 medium neuts yet
[13 28 49] RebecaStorn storyline neuts are too expensive
[13 28 56] Rebeca Storn fit meta 4 then
[13 29 14] Captain Scarz k
[13 29 18] Nianah those 50W neuts
>>>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB