How to parse a comma delimited string when comma a

2019-02-26 01:39发布

I have this string in C#

adj_con(CL2,1,3,0),adj_cont(CL1,1,3,0),NG, NG/CL, 5 value of CL(JK), HO

I want to use a RegEx to parse it to get the following:

adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
NG
NG/CL
5 value of CL(JK)
HO

In addition to the above example, I tested with the following, but am still unable to parse it correctly.

"%exc.uns: 8 hours let  @ = ABC, DEF", "exc_it = 1 day"  , " summ=graffe ", " a,b,(c,d)" 

The new text will be in one string

string mystr = @"""%exc.uns: 8 hours let  @ = ABC, DEF"", ""exc_it = 1 day""  , "" summ=graffe "", "" a,b,(c,d)"""; 

9条回答
小情绪 Triste *
2楼-- · 2019-02-26 02:04

Here's a stronger option, which parses the whole text, including nested parentheses:

string pattern = @"
\A
(?>
    (?<Token>
        (?:
            [^,()]              # Regular character
            |
            (?<Paren> \( )      # Opening paren - push to stack
            |
            (?<-Paren> \) )     # Closing paren - pop
            |
            (?(Paren),)         # If inside parentheses, match comma.
        )*?
    )
    (?(Paren)(?!))    # If we are not inside parentheses,
    (?:,|\Z)          # match a comma or the end
)*? # lazy just to avoid an extra empty match at the end,
    #  though it removes a last empty token.
\Z
";
Match match = Regex.Match(data, pattern, RegexOptions.IgnorePatternWhitespace);

You can get all matches by iterating over match.Groups["Token"].Captures.

查看更多
贪生不怕死
3楼-- · 2019-02-26 02:12

Event faster:

([^,]*\x28[^\x29]*\x29|[^,]+)

That should do the trick. Basically, look for either a "function thumbprint" or anything without a comma.

adj_con(CL2,1,3,0),adj_cont(CL1,1,3,0),NG, NG/CL, 5 value of CL(JK), HO
                  ^                   ^  ^      ^                  ^

The Carets symbolize where the grouping stops.

查看更多
Summer. ? 凉城
4楼-- · 2019-02-26 02:16
var s = "adj_con(CL2,1,3,0),adj_cont(CL1,1,3,0),NG, NG/CL, 5 value of CL(JK), HO";  
var result = string.Join(@"\n",Regex.Split(s, @"(?<=\)),|,\s"));  

The pattern matches for ) and excludes it from the match then matches , or matches , followed by a space.

result =

adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
NG
NG/CL
5 value of CL(JK)
HO

查看更多
登录 后发表回答