How to extract digits between two fixed strings in Arabic Language?

Viewed 109

I have a string in the format:

خصم بقيمة 108 بتاريخ 31-01-2021

And I want to replace the digits between the words: بقيمة & بتاريخ with a "?" character. And keep the digits in the date part of the string

I tried using this Regular Expression: (?<=بقيمة)(.*?)(?=بتاريخ) Which works on https://regex101.com/

But when I implement it in C# in Regex.Replace function, it doesn't have any effect when I use the Arabic words:

    e.Row.Cells[3].Text = Regex.Replace(e.Row.Cells[3].Text, "(?<=بقيمة)(.*?)(?=بتاريخ)", "?");

But it works if I use Latin letters:

    e.Row.Cells[3].Text = Regex.Replace(e.Row.Cells[3].Text, "(?<=X)(.*?)(?=Y)", "?");

Is there anyway to make the function work with Arabic characters? Or is there a better approach I can take to achieve the desired result? For example excluding the date part?

1 Answers

Since the needed digits (without "-"s) are bookended by spaces just use \s(\d+)\s.

var txt = "خصم بقيمة 108 بتاريخ 12-31-2021";
var pattern = @"\s(\d+)\s";
Console.WriteLine( Regex.Match(txt, pattern).Value ); // 108
Related