нужна помощь с Regex. форматирование текста

backoff

Client
Регистрация
20.04.2015
Сообщения
5 926
Благодарностей
6 389
Баллы
113
есть исходник
C#:
H2: Exploring the World of Punjabi Sports
The vibrant state of Punjab boasts a rich sporting culture that reflects its history, traditions, and the indomitable spirit of its people. With a blend of ancient practices and modern influences, Punjabi sports enthusiasts have a diverse range of athletic disciplines to follow and participate in. In this section, we delve into the fascinating world of Punjabi sports, exploring both traditional and modern games that have captured the hearts and minds of millions in the region and beyond.
H3: Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond
Kabaddi, a contact sport that traces its origins to ancient India, has become synonymous with Punjab and its people. Played by teams of seven, kabaddi requires a combination of strength, agility, and strategy, as players try to score points by raiding the opposition's court and tagging opponents. Punjab has produced many kabaddi champions, who have represented India in international competitions and brought home numerous accolades.
Wrestling, also known as kushti or pehlwani, is another traditional sport that has deep roots in Punjabi culture. It is practiced in clay pits called akharas, where aspiring wrestlers train under the guidance of seasoned gurus. The sport is not just a test of physical strength, but also a showcase of mental fortitude and discipline. Apart from kabaddi and wrestling, other traditional sports like gatka (a form of martial arts) and kho-kho have also been an integral part of Punjabi culture for centuries.
H3: Modern Sports with Punjabi Influence: Cricket, Hockey, and Football
Cricket is a national obsession in India, and Punjab is no exception. The state has produced numerous cricketing legends like Harbhajan Singh, Yuvraj Singh, and Kapil Dev, who have etched their names in the annals of the sport. Punjab's domestic cricket teams compete fiercely in various tournaments, including the prestigious Ranji Trophy, while the Punjab Kings represent the state in the Indian Premier League (IPL), one of the biggest T20 cricket leagues in the world.
нужно строки, где расположены теги H2, H3 заменить на html теги, чтоб получилось
C#:
<H2>Exploring the World of Punjabi Sports</H2>
бла бла бла
<H3>Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond</H3>
....
делаю замену
(H2:\ .*)
на
<H2>$1</H2>{-String.Enter-}

получаю
C#:
<H2>Exploring the World of Punjabi Sports
</H2>

The vibrant state of Punjab boasts a rich sporting culture that reflects its history, traditions, and the indomitable spirit of its people. With a blend of ancient practices and modern influences, Punjabi sports enthusiasts have a diverse range of athletic disciplines to follow and participate in. In this section, we delve into the fascinating world of Punjabi sports, exploring both traditional and modern games that have captured the hearts and minds of millions in the region and beyond.
<H3>Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond
</H3>

Kabaddi, a contact sport that traces its origins to ancient India, has become synonymous with Punjab and its people. Played by teams of seven, kabaddi requires a combination of strength, agility, and strategy, as players try to score points by raiding the opposition's court and tagging opponents. Punjab has produced many kabaddi champions, who have represented India in international competitions and brought home numerous accolades.
Wrestling, also known as kushti or pehlwani, is another traditional sport that has deep roots in Punjabi culture. It is practiced in clay pits called akharas, where aspiring wrestlers train under the guidance of seasoned gurus. The sport is not just a test of physical strength, but also a showcase of mental fortitude and discipline. Apart from kabaddi and wrestling, other traditional sports like gatka (a form of martial arts) and kho-kho have also been an integral part of Punjabi culture for centuries.
<H3>Modern Sports with Punjabi Influence: Cricket, Hockey, and Football
</H3>
почему второй тег </H2>, </H3> соскакивает на новую строку?
 

backoff

Client
Регистрация
20.04.2015
Сообщения
5 926
Благодарностей
6 389
Баллы
113
сделал замену
(H2:\ .*){-String.Enter-}
на
<H2>$1</H2>{-String.Enter-}
сработало

Почему непонятно как обрабатываются \r\n ?
@sergodjan66


UPD: Не работает на другом тексте )
 
Последнее редактирование:

djaga

Client
Регистрация
26.04.2020
Сообщения
505
Благодарностей
1 052
Баллы
93
есть исходник
C#:
H2: Exploring the World of Punjabi Sports
The vibrant state of Punjab boasts a rich sporting culture that reflects its history, traditions, and the indomitable spirit of its people. With a blend of ancient practices and modern influences, Punjabi sports enthusiasts have a diverse range of athletic disciplines to follow and participate in. In this section, we delve into the fascinating world of Punjabi sports, exploring both traditional and modern games that have captured the hearts and minds of millions in the region and beyond.
H3: Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond
Kabaddi, a contact sport that traces its origins to ancient India, has become synonymous with Punjab and its people. Played by teams of seven, kabaddi requires a combination of strength, agility, and strategy, as players try to score points by raiding the opposition's court and tagging opponents. Punjab has produced many kabaddi champions, who have represented India in international competitions and brought home numerous accolades.
Wrestling, also known as kushti or pehlwani, is another traditional sport that has deep roots in Punjabi culture. It is practiced in clay pits called akharas, where aspiring wrestlers train under the guidance of seasoned gurus. The sport is not just a test of physical strength, but also a showcase of mental fortitude and discipline. Apart from kabaddi and wrestling, other traditional sports like gatka (a form of martial arts) and kho-kho have also been an integral part of Punjabi culture for centuries.
H3: Modern Sports with Punjabi Influence: Cricket, Hockey, and Football
Cricket is a national obsession in India, and Punjab is no exception. The state has produced numerous cricketing legends like Harbhajan Singh, Yuvraj Singh, and Kapil Dev, who have etched their names in the annals of the sport. Punjab's domestic cricket teams compete fiercely in various tournaments, including the prestigious Ranji Trophy, while the Punjab Kings represent the state in the Indian Premier League (IPL), one of the biggest T20 cricket leagues in the world.
нужно строки, где расположены теги H2, H3 заменить на html теги, чтоб получилось
C#:
<H2>Exploring the World of Punjabi Sports</H2>
бла бла бла
<H3>Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond</H3>
....
делаю замену
(H2:\ .*)
на
<H2>$1</H2>{-String.Enter-}

получаю
C#:
<H2>Exploring the World of Punjabi Sports
</H2>

The vibrant state of Punjab boasts a rich sporting culture that reflects its history, traditions, and the indomitable spirit of its people. With a blend of ancient practices and modern influences, Punjabi sports enthusiasts have a diverse range of athletic disciplines to follow and participate in. In this section, we delve into the fascinating world of Punjabi sports, exploring both traditional and modern games that have captured the hearts and minds of millions in the region and beyond.
<H3>Traditional Sports in Punjab: Kabaddi, Wrestling, and Beyond
</H3>

Kabaddi, a contact sport that traces its origins to ancient India, has become synonymous with Punjab and its people. Played by teams of seven, kabaddi requires a combination of strength, agility, and strategy, as players try to score points by raiding the opposition's court and tagging opponents. Punjab has produced many kabaddi champions, who have represented India in international competitions and brought home numerous accolades.
Wrestling, also known as kushti or pehlwani, is another traditional sport that has deep roots in Punjabi culture. It is practiced in clay pits called akharas, where aspiring wrestlers train under the guidance of seasoned gurus. The sport is not just a test of physical strength, but also a showcase of mental fortitude and discipline. Apart from kabaddi and wrestling, other traditional sports like gatka (a form of martial arts) and kho-kho have also been an integral part of Punjabi culture for centuries.
<H3>Modern Sports with Punjabi Influence: Cricket, Hockey, and Football
</H3>
почему второй тег </H2>, </H3> соскакивает на новую строку?
Привет.
Нужно использовать флаг Single line и немного подправить ваше регулярное выражение.)

Короткое решение:
Исходный текст хранится в переменной (ZM) с именем "text".
Результат кладется в указанной вами переменной с помощью кубика C#.
[LINQ] Поиск и замена всех тегов (от 1 до 5):
return Enumerable.Range(1, 5).Aggregate(project.Variables["text"].Value, (current, i) =>
    Regex.Replace(current, $@"H{i}: (.*?)(?=H[\d]:|\nH[\d]:|$)", $"<H{i}>$1</H{i}>",
        RegexOptions.Singleline));
Понятное решение:
Поиск и замена всех тегов (от 1 до 5):
string textInProgress = project.Variables["text"].Value;
for (int i = 1; i <= 5; i++)
{
    string pattern = $@"H{i}: (.*?)(?=H[\d]:|\nH[\d]:|$)";
    string replacement = $"<H{i}>$1</H{i}>";
    textInProgress = Regex.Replace(textInProgress, pattern, replacement, RegexOptions.Singleline);
}
return textInProgress;
Шаблон для тестов приложил ↓
 

Вложения

Последнее редактирование:

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)