Regular expression - replace in groups
-
How can C# be replaced by regular expressions in the found group? Example: In the row, the collateral (,) should be replaced by collateral points (;) in brackets only.
string text = @"магазин Продукты, магазин Запчасти (эмали, лаки), магазин Автотовары (запчасти, комплектующие, разборка)";
I've got a trail to find.
@"\((.*)\)"
♪ I can pick groups. How do you replace and put it in one final line?
-
We can use it. https://msdn.microsoft.com/ru-ru/library/ht1sxswy(v=vs.110).aspx with parameter
MatchEvaluator evaluator
♪ In addition, a lazy search should be used, or in the first group all the symbols between the first bracket in the line and the last bracket in the line.Example
string text = @"магазин Продукты, магазин Запчасти (эмали, лаки), магазин Автотовары (запчасти, комплектующие, разборка)"; string pattern = @"\(.*?\)"; var result = Regex.Replace(text, pattern, (m) => { // возвращаемое значение вставляется вместо обнаруженной группы return m.Value.Replace(",", ";"); });