[英]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]*\))?
注意:
“:”之前的文本(半冒號作為字典的值)
如果您有任何疑問,請告訴我
我不會使用正則表達式。 我將執行以下操作:
dictionary<string,list<string>>
類的持有對象 哦,您可能需要在其中添加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.