Пример текст: blahblahblah ~ Start Capture ~ yadda ~ yadda ~ yadda ~ loop1 hello1 ~ loop2 hello2 ~ loop3 hello3 ~ end stuff ~ < /p>
regex: (? /> Желаемый вывод: Blahblahblah ~ Start Capture ~ yadda ~ yadda ~ yadda ~ loop3 hello3 ~ loop2 hello2 ~ loop1 hello1 ~ end stuff ~ < /p>
var Regex = new System.Text.RegularExpressions.Regex(@"(?~Start[^~]*)(?.*?)(?(?:~(?:LOOP)\d[^~]*)(.*?))+(?~End stuff~)");
var Text = "BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~LOOP3 hello3~End stuff~";
// This is the wrong syntax to express the loops group capture collection.
//Note:${loops{2}} is also wrong
var Output = Regex.Replace(Text, "${firstCapture}${randomStuff}${loops(2)}${loops(1)}${loops(0)}${end}");
//BlahBlahBlah~Start capture~Yadda~Yadda~Yadda${loops(2)}${loops(1)}${loops(0)}~End stuff~
// This works, just wanting to get it in one bite if possible.
var MyMatch = Regex.Match(Text);
var Output2 = Regex.Replace(Text, $"${{firstCapture}}${{randomStuff}}{MyMatch.Groups("loops").Captures(2)}{MyMatch.Groups("loops").Captures(1)}{MyMatch.Groups("loops").Captures(0)}${{end}}");
//BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP3 hello3~LOOP2 hello2~LOOP1 hello1~End stuff~
Подробнее здесь: https://stackoverflow.com/questions/796 ... e-collecti
.NET recex replyx - Как обратиться к отдельному элементу коллекции захвата в замене? ⇐ C#
Место общения программистов C#
1750197051
Anonymous
Пример текст: blahblahblah ~ Start Capture ~ yadda ~ yadda ~ yadda ~ loop1 hello1 ~ loop2 hello2 ~ loop3 hello3 ~ end stuff ~ < /p>
regex: (? /> Желаемый вывод: Blahblahblah ~ Start Capture ~ yadda ~ yadda ~ yadda ~ loop3 hello3 ~ loop2 hello2 ~ loop1 hello1 ~ end stuff ~ < /p>
var Regex = new System.Text.RegularExpressions.Regex(@"(?~Start[^~]*)(?.*?)(?(?:~(?:LOOP)\d[^~]*)(.*?))+(?~End stuff~)");
var Text = "BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~LOOP3 hello3~End stuff~";
// This is the wrong syntax to express the loops group capture collection.
//Note:${loops{2}} is also wrong
var Output = Regex.Replace(Text, "${firstCapture}${randomStuff}${loops(2)}${loops(1)}${loops(0)}${end}");
//BlahBlahBlah~Start capture~Yadda~Yadda~Yadda${loops(2)}${loops(1)}${loops(0)}~End stuff~
// This works, just wanting to get it in one bite if possible.
var MyMatch = Regex.Match(Text);
var Output2 = Regex.Replace(Text, $"${{firstCapture}}${{randomStuff}}{MyMatch.Groups("loops").Captures(2)}{MyMatch.Groups("loops").Captures(1)}{MyMatch.Groups("loops").Captures(0)}${{end}}");
//BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP3 hello3~LOOP2 hello2~LOOP1 hello1~End stuff~
Подробнее здесь: [url]https://stackoverflow.com/questions/79669785/net-regex-replace-how-to-refer-to-an-individual-element-of-a-capture-collecti[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия