繁体   English   中英

C# 正则表达式替换希腊字母

[英]C# Regex Replace Greek Letters

我得到像“thetaetaA”(theta eta A)这样的字符串我需要替换接收到的字符串,如 {\theta}{\eta}A

// C# code with regex to match greek letters
string gl = "alpha|beta|delata|theta|eta";
string recived = "thetaetaA";
var greekLetters = Regex.Matches(recived,gl);

有人可以告诉我如何创建所需的文本 {\theta}{\eta}A

如果我使用循环并执行替换它会生成以下输出

{\th{\eta}}{\eta}A

因为 theta 包括 eta

Regex.Matches()不会取代任何东西。 使用Regex.Replace() 捕获单词并在替换中引用捕获,并在其周围添加特殊字符。 (并且可能在交替中的子字符串之前有超字符串。虽然它对我有用。据说无论如何它都是一个贪婪的匹配。)

class Program
{
    static void Main(string[] args)
    {
        string gl = "alpha|beta|delta|theta|eta";
        string received = "thetaetaA";
        string texified = Regex.Replace(received, $"({gl})", @"{\$1}");

        Console.WriteLine(texified);

        Console.ReadKey();
    }
}

添加您的代码RegexOptions.IgnoreCase

var greekLetters = Regex.Matches(recived, gl, RegexOptions.IgnoreCase);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM