0

Необходимо найти в тексте предложение в одинарных кавычках, которые могут содержать знаки препинания и числа. Перед закрывающей кавычкой стоит один из трех символов . ? ! Ввод:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 'Ut enim ad minim veniam, quis 45 nostrud exercitation ullamco laboris nisi ut, aliquip ex ea commodo consequat.' Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Вывод:

'Ut enim ad minim veniam, quis 45 nostrud exercitation ullamco laboris nisi ut, aliquip ex ea commodo consequat.'

Мой код (игнорирует запятые):

Regex regex = new Regex(@"\'[\w\s]+[.!?]?\'");
MatchCollection matches = regex.Matches(text);
foreach (Match match in matches)
{
    Console.WriteLine(match);
}
3
  • а чем именно ваш код вам не подходит? 21 июл 2019 в 12:06
  • @ВиталийШебаниц предложение с запятыми взять не получается.
    – Sergey
    21 июл 2019 в 12:08
  • var results = Regex.Matches(text, @"'[^']*[?!.]'").Cast<Match>().Select(x => x.Value).ToList(); 22 июл 2019 в 21:14

1 ответ 1

0
Regex regex = new Regex(@"\'[\w\s]+[.!?]?\'");
Regex regex = new Regex("'.*?[.?!]'");
3
  • А так, чтобы он не брал все подряд?
    – Sergey
    21 июл 2019 в 12:15
  • 7
    и опять ни строчки пояснений. может, всё-таки стоит их добавить? 21 июл 2019 в 12:27
  • @Gendalf, а если заменить точку на [^']?
    – Qwertiy
    21 июл 2019 в 21:08

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.