English 中文(简体)
How to write regex to split a string by commas, with some exceptions?
原标题:

I ve been learning regular expressions for the last few days and I ve been stumbled on this one. I would like to split a string by commas but with exceptions. Here s my string on what I want to validate:

rp { av 100, re 3, 52 }

which is basic comma delimited values inside of curly braces. Getting the values, I can do. But inside of each values, there could be another

rp { values, values values }

which I don t want it to catch. This can be recursive.


String to evaluate:

rp { av 100, re 3, rp { value 1, value 2, value 3 }, 52 }

Desired match:

av 100
re 3
rp { value 1, value 2, value 3 }
52
最佳回答

It s not clear to me if this example represents a potential infinite nesting of these JSON-esque groups, or just one level of nesting.

If only one level of nesting is possible, a straightforward Regex will do:

(Please note I did not write this with an IDE; the concept should be correct but syntax errors can t be guaranteed at the moment. Apologies)

string pattern = @"(?<key>)[A-Z]+)s(?<value>({.+?}|[^{},]+))";

List<string[]> results = new List<string[]>(); //probably not best data structure

MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.SingleLine | RegexOptions.IgnoreCase);
foreach(Match match in matches)
{
    if(match.Success)
    {
        results.Add(new string[] {
            match.Groups["key"].Value,
            match.Groups["value"].Value
        });
    }
}

If they can be nested many levels, you probably want to approach this recursively. That would necessitate splitting the value match into nested value and simple value:

string pattern = @"(?<key>)[A-Z]+)s({(?<nested>.+?)}|(?<simple>[^{},]+))";

And for each match where nested has a value, execute the same routine against that value:

void Deserialize(string input, List<string[]> values)
{
    MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.SingleLine | RegexOptions.IgnoreCase);
    foreach(Match match in matches)
    {
        if(match.Success)
        {
            if(match.Groups["nested"].Success && !string.IsNullOrEmpty(match.Groups["nested"].Value))
            {
                Deserialize(match.Groups["nested"].Value, values);
            }
            else
            {
                values.Add(new string[] {
                    match.Groups["key"].Value,
                    match.Groups["simple"].Value
                });
            }
        }
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签