簡體   English   中英

使用正則表達式捕獲分隔符內的子字符串並排除字符

[英]Capture substring within delimiters and excluding characters using regex

正則表達式模式如何捕獲兩個分隔符之間的子字符串,但在第一個分隔符之后和最后一個分隔符(如果有)之前排除某些字符(如果有)? 輸入字符串看起來像這樣:

var input = @"Not relevant {

#AddInfoStart Comment:String:=""This is a comment"";

AdditionalInfo:String:=""This is some additional info"" ;

# } also not relevant";

捕獲應包含“{”和“}”之間的子字符串,但不包括任何空格、換行符和起始分隔符“{”之后的“#AddInfoStart”字符串(只要它們中的任何一個存在),並且不包括任何空格、換行符和“;” 和結束分隔符“}”之前的“#”字符(如果存在任何字符也是如此)。

捕獲的字符串應如下所示

Comment:String:=""This is a comment"";

AdditionalInfo:String:=""This is some additional info""

“:”和“:=”內部分隔符之前或之后可能有空格,而且“:=”之后的值並不總是標記為字符串,例如:

{  Val1 : Real := 1.7  }

對於數組,使用以下語法:

arr1 : ARRAY [1..5] OF INT := [2,5,44,555,11];
arr2 : ARRAY [1..3] OF REAL

這是我的解決方案:

  1. 刪除括號外的內容
  2. 使用正則表達式獲取括號內的值

代碼:

var input = @"Not relevant {

#AddInfoStart Comment:String:=""This is a comment"";

            Val1 : Real := 1.7

AdditionalInfo:String:=""This is some additional info"" ;

# } also not relevant";

// remove content outside brackets
input = Regex.Replace(input, @".*\{", string.Empty);
input = Regex.Replace(input, @"\}.*", string.Empty);

string property = @"(\w+)"; 
string separator = @"\s*:\s*"; // ":" with or without whitespace
string type = @"(\w+)"; 
string equals = @"\s*:=\s*"; // ":=" with or without whitespace
string text = @"""?(.*?)"""; // value between ""
string number = @"(\d+(\.\d+)?)"; // number like 123 or with a . separator such as 1.45
string value = $"({text}|{number})"; // value can be a string or number
string pattern = $"{property}{separator}{type}{equals}{value}";

var result = Regex.Matches(input, pattern)
                  .Cast<Match>()
                  .Select(match => new
                  {
                      FullMatch = match.Groups[0].Value, // full match is always the 1st group
                      Property = match.Groups[1].Value, 
                      Type = match.Groups[2].Value, 
                      Value = match.Groups[3].Value 
                  })
                  .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM