Regular expression to trim only the last () of a sentence

It’s hard for me to find a regular expression. I have suggestions: "1A11 - Car engine control unit (VECU) (behind the plate)" "1A1K5 - rear view of the car (front view)" I want to trim my sentence from (----), I have this regular expression to do this "@" \ s * ([^)] *), but the problem with this is that, as in my first sentence, (VECU) is an abbreviation, so I need to keep it, but it is regular expression doesn't work if i have 2 () (). How can I change my regex 2 to trim only the last () of a sentence?

 if (!reportMode)
 {
     //Look line by line for Title                               
     stream = GetStream(files);
     List<String> fileContent = new List<String>();
     using (StreamReader sr = new StreamReader(stream))
     {
        String line = "";
        Boolean isInThere = false;
        while (!sr.EndOfStream)
        {
           line = sr.ReadLine();
           if (line.Contains(title))
           {
              //check for exact match
              Int32 index = line.IndexOf(" - ");
              String revisedLine = line.Substring(index + 3).Trim();
              String str = Regex.Replace(revisedLine, @"\s*\([^\)]*\)", "").Trim();
              if (Regex.IsMatch(str, String.Format("^{0}$", title)))
                  isInThere = true;
           }
           fileContent.Add(line);
      }
+4
source share
2 answers

. '$' : "\ s *\([^ \)] * \) $". , . .

( regexp, )

- Maxp

+1

, , ,

Regex rx = new Regex(@"\s*\([^()]*\)(?=[^()]*$)");
String str = rx.Replace(revisedLine, "").Trim();

REGEX:

  • \s* - 0
  • \([^()]*\) - , , ) (
  • (?=[^()]*$) - , , ( ).

, .

+1

All Articles