Home > Back-end >  Regular expression find string outside square brackets
Regular expression find string outside square brackets

Time:12-18

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

Sample output

CodePudding user response:

Not quite what you asked for, but I'd recommend something like this:

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

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.

  • Related