简体   繁体   English

正则表达式在方括号外查找字符串

[英]Regular expression find string outside square brackets

I have following sample data.我有以下示例数据。 The end goal is to translate (to another language like say Spanish) on the fly data that's outside the square brackets.最终目标是即时翻译方括号外的数据(如西班牙语等其他语言)。

Below code works for most the cases however it fails for the last data for obvious reasons when the brackets [] are inside the string.下面的代码适用于大多数情况,但是当括号 [] 在字符串内时,由于明显的原因,它对于最后一个数据失败。

So I am wondering how to write an expression which will only grab things which are outside the brackets.所以我想知道如何编写一个只会抓住括号外的东西的表达式。 Complete opposite of what I am doing right now.与我现在正在做的完全相反。 Please keep in mind that there can be messages that don't have any square brackets at all.请记住,可能存在根本没有任何方括号的消息。 You can assume that opening bracket will always have closing bracket.您可以假设左括号总是有右括号。

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();
        }
    }
}

样本输出

Not quite what you asked for, but I'd recommend something like this:不完全符合您的要求,但我建议您这样做:

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 match = regEx.Match(instruction);
               var instructionsOnly = regEx.Replace(instruction, placeholder).Trim();
               var newInstruction = "'This is now Spanish: " + instructionsOnly + "'";
               var newFinalValue = newInstruction.Replace(placeholder, match.Value);
               Console.WriteLine(newFinalValue);
            }

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

Oops, this is wrong;糟糕,这是错误的; didn't see at first that there can be multiple bracketed expressions.起初没有看到可以有多个括号表达式。 Give me a minute.给我一点时间。

I have some experience with machine translation services, and usually they leave all-caps word mashups alone.我在机器翻译服务方面有一些经验,通常他们不理会全大写单词混搭。 But you could try a sequence of symbols or whatever seems to work with your particular service.但是您可以尝试一系列符号或任何似乎适用于您的特定服务的符号。

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

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