REGEX: Как игнорировать оглавление при переносе текста между двумя главами? - PullRequest
0 голосов
/ 05 октября 2019

Я хочу получить содержимое в Главе 1, но вместо этого он продолжает получать оглавление.

Я пробовал это Chapter 1((.*\n)*)Chapter 2, но это захватило бы оглавление. Если я вручную удалю оглавление, оно будет работать нормально.

Полный текст:

1. Chapter 1
2. Chapter 2
3. Chapter 3

Chapter 1
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum\

Chapter 2
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum

1 Ответ

2 голосов
/ 05 октября 2019

Если вы используете разновидность регулярного выражения, поддерживающую lookbehind, вы можете использовать отрицательный lookbehind для \d. перед Chapter 1, чтобы избежать совпадения Chapter 1 в оглавлении. Включив Chapter 1 в качестве положительного взгляда за спиной и используя положительный прогноз для Chapter 2, вы можете сделать желаемый текст полностью совпадающим:

(?<=(?<!\d. )Chapter 1).*(?=Chapter 2)

Демонстрация для regex101

Обратите внимание, что вам нужно использовать флаг s, чтобы . соответствовал символу новой строки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...