Как я могу разделить символ новой строки и два символа новой строки в регулярном выражении? - PullRequest
0 голосов
/ 06 февраля 2019

Я хочу сгруппировать вывод регулярного выражения:

  1. символ новой строки '\ n'
  2. два символа новой строки '\ n \ n'

Как я могу разделить на 2 группы, чтобы использовать другой метод разделения регулярных выражений?

Найти отдельную новую строку или две строки, которыми я управлял.Например:

Facebook and Google exploited a feature__(\n)__  
intended for “enterprise developers” to__(\n)__  
distribute apps that collect large amounts__(\n)__  
of data on private users, TechCrunch first reported.__(\n\n)__   

Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power.__(\n)__  
Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?__(\n\n)__  

Some text so on... 

Я попробовал этот код:

def find_newlines(file):
    with open(file, "r") as content:
       text = content.read()
       content = re.split("\n+", text)
    return content

Результат был:

['Apple' , 'Something', 'Enything']

Я хотел следующий вывод:

['Facebook and Google exploited a feature intended for “enterprise developers” to distribute apps that collect large amounts of data on private users, TechCrunch first reported.' __,__ 'Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power. Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?']

Я хочу получить 1 группу символов новой строки и 2 группы из двух строк новой строки.

1 Ответ

0 голосов
/ 06 февраля 2019

Вы, кажется, пытаетесь сгруппировать ваш текст в два (или более) блока, разделенных двойными символами новой строки.Один из таких подходов - сначала разбить текст на \n\n.Это приведет к blocks, которые все еще содержат одиночные переводы строки.Каждый блок может затем заменить любые оставшиеся символы новой строки пробелами.Все это можно сделать, используя понимание списка Python следующим образом:

text = """Facebook and Google exploited a feature
intended for “enterprise developers” to
distribute apps that collect large amounts
of data on private users, TechCrunch first reported.

Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power.
Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?"""

content = [block.replace('\n', ' ') for block in text.split('\n\n')]

print(content)

Предоставление вам списка с двумя записями и без перевода строки:

['Facebook and Google exploited a feature intended for “enterprise developers” to distribute apps that collect large amounts of data on private users, TechCrunch first reported.', 'Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power. Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?']

Можно использовать регулярное выражениеиспользуется для случая, когда блоки разделены двумя или более пустыми строками следующим образом:

import re

text = """Facebook and Google exploited a feature
intended for “enterprise developers” to
distribute apps that collect large amounts
of data on private users, TechCrunch first reported.



Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power.
Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?"""

content = [block.replace('\n', ' ') for block in re.split('\n{2,}', text)]

print(content)
...