【问题标题】:Regular expression find string outside square brackets正则表达式在方括号外查找字符串
【发布时间】:2022-01-20 16:16:26
【问题描述】:

我有以下示例数据。最终目标是即时翻译方括号外的数据(如西班牙语等其他语言)。

下面的代码适用于大多数情况,但是当括号 [] 在字符串内时,由于明显的原因,最后一个数据会失败。

所以我想知道如何编写一个只抓取括号外的东西的表达式。与我现在正在做的完全相反。请记住,可能存在根本没有任何方括号的消息。你可以假设左括号总是有右括号。

namespace RegExDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] data = new string[]
            {
                "[00:05:00] Insert buckle tongue [0/1 = 5.81mA]", 
                "Remove buckle tongue [1/1 = 5.81mA]", 
                "Move track forward",
                "Move track forward [MinCrt: 1.0, 0.1A, MaxCrt: 5.0] [MinPos: 450mm, 420, 520mm]",
                "Waiting before taking reading [500ms]",
                "Waiting [500ms] before taking reading"
            };

            var regEx = new Regex(@"\[(.*?)\]");
            foreach (var instruction in data)
            {
                var instructionsOnly = regEx.Replace(instruction, string.Empty).Trim();
                var newInstruction = "'This is now Spanish: " + instructionsOnly + "'";
                var newFinalValue = instruction.Replace(instructionsOnly, newInstruction);
                Console.WriteLine(newFinalValue);
            }

            Console.WriteLine("All done");
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 实际上您希望使用@"(\[.*?\])" 拆分字符串。然后,循环数组,如果它匹配@"^\[.*?\]$",则不理会元素。如果没有,请将元素转换为西班牙语。完成后,将数组连接成一个字符串。
  • 实际上,最好用占位符一次性翻译整个字符串,因为翻译时词序会发生变化。

标签: c# regex


【解决方案1】:

不完全符合您的要求,但我建议您这样做:

using System.Text.RegularExpressions;

namespace RegExDemo
{
   class Program
   {
      static void Main(string[] args)
      {
         string[] data = new string[]
         {
                                "[00:05:00] Insert buckle tongue [0/1 = 5.81mA]",
                                "Remove buckle tongue [1/1 = 5.81mA]",
                                "Move track forward",
                                "Move track forward [MinCrt: 1.0, 0.1A, MaxCrt: 5.0] [MinPos: 450mm, 420, 520mm]",
                                "Waiting before taking reading [500ms]",
                                "Waiting [500ms] before taking reading"
         };

         var regEx = new Regex(@"\[.*?\]"); // lose the parentheses
         var placeholder = "SOMETHINGTHETRANSLATIONSERVICECANBETRUSTEDNOTTOTRYTOTRANSLATE";

         foreach (var instruction in data)
         {
            var matches = regEx.Matches(instruction);
            string tmp = instruction;
            for (int i = 0; i < matches.Count; i++)
            {
               tmp = tmp.Replace(matches[i].Value, $"{placeholder}{i:0000}");
            }
            var newInstruction = "'This is now Spanish: " + tmp + "'";
            for (int i = 0; i < matches.Count; i++)
            {
               newInstruction = newInstruction.Replace($"{placeholder}{i:0000}", matches[i].Value);
            }
            var newFinalValue = newInstruction;
            Console.WriteLine(newFinalValue);
         }

         Console.WriteLine("All done");
         Console.ReadLine();
      }
   }
}

我在机器翻译服务方面有一些经验,通常他们不会使用全大写单词混搭。但是您可以尝试一系列符号或任何似乎适用于您的特定服务的符号。

【讨论】:

  • 几件事:1)instruction.Replace(matches[i].Value, $"{placeholder}{i:0000}"); 替换后新生成的字符串不存储/使用 2)当数据有多个括号 [] 时,通过从正则表达式中丢失括号,它将合并所有数据到一个字符串。如果我误解了什么,请告诉我。
  • 我不确定我是否遵循您的任何观点。正如发布的那样,这对我有用。显然,var newInstruction = "'This is now Spanish: " + instruction + "'"; 行是调用翻译服务的地方。
  • 哦,我明白你在#1 上的观点。对不起,我很厚。现在已经修好了。
猜你喜欢
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多