簡體   English   中英

正則表達式2

[英]Regular Expression #2

我如何解析以下內容?

data (DIR1:input bit;
      DGG2:input bit;
      OENEG1:input bit;
      OE_NEG2:input bit;
      A1:inputoutput bit_vector(1 to 9);
      A2,H5,J7:inputoutput bit_vector(1 to 9);
      B1,E4,Y7:inputoutput bit_vector(1 to 9);
      B2:inputoutput bit_vector(1 to 9);
                TGY:output bit;
      THHH, Tff, TsD:input bit);

我想要字典中的輸出,如下所示

 Dictionary<string,string> l_dictData = new Dictionary<string,string>();

解析后, l_dictData應該填充結果:

 l_dictData["inputbit"] = "DIR1,DGG2,OENEG1,OE_NEG2,THHH,Tff,TsD";

 l_dictData["inputoutputbit"] = "A1(1),A1(2),....,A1(9)A2(1),A2(2)....A2(9),H5(1)....H5(9),J7(1),...J7(9),B1(1),....B1(9),E4(1),....E4(9),Y7(1),...Y7(9),B2(1),....B2(9)";

 l_dictData["outputbit"] = "TGY";

這是我的正則表達式

    1. ([ \t\r\n]*)?(data|DATA)([ \t\r\n]*)?(\()?
    2.  "[ \t\r\n]*(?<PINFUNC>(inputbit|outputbit|inputoutputbit))(_vector[ \t\r\n]*\([ \t\r\n]*(?<START>([0-9]+))[ \t\r\n]*(to|downto)[ \t\r\n]*(?<END>([0-9]+))[ \t\r\n]*\))?

注意:

“:”之前的文本(半冒號作為字典的值)

如果您有任何疑問,請告訴我

我不會使用正則表達式。 我將執行以下操作:

  1. 過濾掉括號中的內容。
  2. 把你的弦分開; 獲得個人價值。
  3. 創建一個類似dictionary<string,list<string>>類的持有對象
  4. 遍歷您的每個名稱/值事物(例如“ DIR1:輸入位”)並分割為:
  5. 計算出您的鍵和值(您的鍵似乎與“:”后的鍵不完全匹配
  6. 如果key在字典中,則將值添加到列表中;如果key不在字典中,則需要首先創建字符串列表。
  7. 完成引用值列表的字典的循環。
  8. 只需將列表轉換為單個字符串,即可遍歷新詞典並將值寫入最終詞典。
  9. 利潤。

哦,您可能需要在其中添加trim()來處理空白。

此表達式: (?:\\(|\\s)\\s*([\\w| |,]*):(\\w*?) bit.*?;

產生以下結果:

[1] => Array
    (
        [0] => DIR1
        [1] => DGG2
        [2] => OENEG1
        [3] => OE_NEG2
        [4] => A1
        [5] => A2,H5,J7
        [6] => B1,E4,Y7
        [7] => B2
        [8] => TGY
        [9] => THHH, Tff, TsD
    )

[2] => Array
    (
        [0] => input
        [1] => input
        [2] => input
        [3] => input
        [4] => inputoutput
        [5] => inputoutput
        [6] => inputoutput
        [7] => inputoutput
        [8] => output
        [9] => input
    )

按逗號分割,修剪空格,在鍵上添加“位”,操作完成。

多虧了我的Regex Tester(如果您要求的話,它也會對此進行解釋): http : //www.myregextester.com/index.php

暫無
暫無

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

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