开发者

C# : Regular Expression

开发者 https://www.devze.com 2023-02-02 16:09 出处:网络
I\'m having a set of row data as follows List<String> l_lstRowData = new List<string> { \"Data 1 32:01805043*0FFFFFFF\",

I'm having a set of row data as follows

 List<String> l_lstRowData = new List<string> { "Data 1 32:01805043*0FFFFFFF", 
                                                "Data 3, 20.0e-3", 
                                                "Data 2, 1.0e-3 172:?:CRC" ,
                                                "Data 6"
                                              };

and two List namely "KeyList" and "ValueList" like

 List<string> KeyList = new List<string>();
 List<string> ValueList = new List<string>();

I need to fill the two List<String> from the data from l_lstRowData using Pattern Matching

And here is my Pattern for this

 String l_strPattern = @"(?<KEY>(Data|data|DATA)\s[0-9]*[,]?[ ]?[0-9e.-]*)[ \t\r\n]*(?<Value>[0-9A-Za-z:?*!. \t\r\n\-]*)";
 Regex CompiledPattern=new Regex(l_strPattern,RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

So finally the two Lists will contain

 KeyList 

            { "Data 1" }
            { "Data 3, 20.0e-3" }
            { "Data 2, 1.0e-3" }
            { "Data 6" }



 ValueList 

            { "32:01805043*0FFFFFFF" }
            { "" }
            { "172:?:CRC" }
            { "" }

Scenerio:

The Group KEY in the Pattern Should match "The data followed by an integer value 开发者_Go百科, and the if there exist a comma(,) then the next string i.e a double value

The Group Value in the Pattern should match string after the whitespace.In the first string it should match 32:01805043*0FFFFFFF but in the 3rd 172:?:CRC.

Here is my sample code

 for (int i = 0; i < l_lstRowData.Count; i++)
        {
            MatchCollection M = CompiledPattern.Matches(l_lstRowData[i], 0);
            KeyList.Add(M[0].Groups["KEY"].Value);
            ValueList.Add(M[0].Groups["Value"].Value);

        }

But my Pattern is not working in this situation.

EDIT

My code result like

 KeyList 

            { "Data 1 32" } // 32 should be in the next list
            { "Data 3, 20.0e-3" }
            { "Data 2, 1.0e-3" }
            { "Data 6" }



 ValueList 

            { ":01805043*0FFFFFFF" }
            { "" }
            { "172:?:CRC" }
            { "" }

Please help me to rewrite my Pattern.


Your code works for me, so please define what's not working.

Also:

  • start your regexp with ^ and end it with $
  • use regex.Match() instead of Matches() because you know it'll only match once
  • i don't see why you use IgnorePatternWhitespace
  • use a simple comma instead of [,], a simple space instead of [ ]
  • use \s instead of [ \t\r\n]
  • if you specify IgnoreCase, then no need for Data|data|DATA or [A-Za-z]

And if you clean this up, maybe you can solve it alone :)


I think a simpler regex would work: (?<key>data \d(?:, [\d.e-]+)?)(?<value>.*) will match your keys and values, providing you use the RegexOptions.IgnoreCase flag too.

You can see the results at this Rubular example link.

0

精彩评论

暂无评论...
验证码 换一张
取 消