2

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?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Danya B
  • 29
  • 3
  • See also: https://stackoverflow.com/questions/65805812/system-uri-drops-unicode-rlm-right-to-left-mark-u200f-character-in-net-4-5/65927071#65927071 I presume the problem is that a hidden right-to-left mark (or other bidirectional control character) is present in the Regex and/or your input string. – Peter O. Feb 04 '21 at 07:28
  • I tried the suggested solution and using the ToggleIDNIRISupport method but it didn't work. – Danya B Feb 04 '21 at 09:08
  • Of course the ToggleIDNIRISupport method didn't work; it applies only to URI methods which have nothing to do with regex. I suggested the link to the question above because your problem and the problem in that question may have the same root cause: the use of bidirectional control characters. It may be possible that the Arabic strings differ in terms of code points (the `char`s in each `string`) in a subtle way. – Peter O. Feb 04 '21 at 10:12
  • [It works](https://ideone.com/a4S9My) fine. – Wiktor Stribiżew Feb 04 '21 at 21:49

1 Answers1

-1

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
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122