String Splitter in .NET

I have a line below

P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO

I need a conclusion like

P

MV

A1ZWR

MAV#(X,,)

PV

MOV#(X,12,33)

LO

How can you understand that this can be easily done by breaking it into "," but the problem comes

when it is type MAV # (X,) or MOV # (X, 12.33).

Please, help

+5
source share
10 answers

Since there was no such solution, using only LINQ, and I was wondering what it would look like, I came up with this. But I would not recommend using it in production code. Actually, I was hoping this would be better, but since the nested parent elements needed to be processed, I had to introduce state variable variables.

string data = "P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO";

int depth = 0;
int group = 0;

var result = data
    .GroupBy(x => { 
        if (x == '(') depth++;
        if (x == ')') depth--;
        if (x == ',' && depth == 0) group++; 
        return group; })
    .Select(x => new String(x.ToArray()).Trim(' ', ','))
+4
source

, . :

string data = "P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO";

foreach (Match m in Regex.Matches(data, @"\s*((\(.*?\)|[^,])*)(,|$)")) {
  Console.WriteLine(m.Groups[1].Value);
}

:

P
MV
A1ZWR
MAV#(X,,)
PV
MOV#(X,12,33)
LO
+8
string input = "P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO";
IList<string> parts = new List<string>();
int paranthesisCount = 0;
int lastSplitIndex = 0;
for (int i = 0; i < input.Length; i++)
{
    if (input[i] == '(')
    {
        paranthesisCount++;
        continue;
    }
    if (input[i] == ')')
    {
        paranthesisCount--;
        continue;
    }
    if (input[i] == ',' && paranthesisCount == 0)
    {
        parts.Add(input.Substring(lastSplitIndex, i - lastSplitIndex));
        lastSplitIndex = i + 1;
    }
}
if (input.Length - lastSplitIndex > 0)
{
    parts.Add(input.Substring(lastSplitIndex, input.Length - lastSplitIndex));
}
+4

. CSV, , #(...) "..." .

+2

, ( ):

string[] test = "P,MV,A1ZWR,MAV#(X,,), PV,MOV#(X,12,33),LO".Split(',');

bool insideElement = false;
string insideElementResult = "";
List<string> result = new List<string>();
foreach (string s in test)
{
    //Determine context:
    if (s.IndexOf("(") > -1)
        insideElement = true;

    //Determine where to add my nice string
    if (!insideElement)
        result.Add(s);
    else
        insideElementResult += s;

    //Determine if contact has ended:
    if (s.IndexOf(")") > -1)
    {
        insideElement = false;
        result.Add(insideElementResult);
        insideElementResult = null;
    }
    else if (insideElement)
    {
        insideElementResult += ",";
    }

}

:

    [0] "P" string
    [1] "MV"    string
    [2] "A1ZWR" string
    [3] "MAV#(X,,)" string
    [4] " PV"   string
    [5] "MOV#(X,12,33)" string
    [6] "LO"    string

, , , , , ;)

+1

, Parser. . , ,

  • occour
  • ( , )

xsd .

ANTLR. , . . .

+1

, .

 P~MV~A1ZWR~MAV#(X,,)~ PV~MOV#(X,12,33)~LO

(0x00?)

0

, , , , , | . , , , , , .

If not, you may need to split the lines and look at the buckets, looking for problems and perform any necessary merging and further separation.

0
source

This function will pull out all tokens, make sure that there are no double commas between the tokens, and make sure that all parentheses are closed. It is a little longer.

IEnumerable<string> Tokenise(string input)
{
    const char tokenlimiter = ',';
    const char funcstart = '#';
    const char funcend = ')';
    StringBuilder token = new StringBuilder(5);
    bool gotfunc = false;
    bool gotone = false;
    int pos = 0;
    int opened = 0;
    foreach(char c in input)
    {
        if (c == funcstart)
        {
            gotfunc = true;
            opened++;
        }
        if(c == funcend)
        {
            gotfunc = false;
            opened--;
        }
        if(!gotfunc && c == tokenlimiter)
        {
            gotone = true;
            if(token.Length == 0)
            {
                throw new ArgumentException("Blank instruction at " + pos, input);
            }
            yield return token.ToString();
        }
        if(gotone)
        {
            token = new StringBuilder(5);
            gotone = false;
        }
        else
        {
            token.Append(c);    
        }
        if(pos == input.Length - 1)
        {
            if (!gotfunc && opened == 0 && c != tokenlimiter)
            {
                yield return token.ToString();
            }
            else if (gotfunc || opened != 0)
            {
                throw new ArgumentException("Broken function", input);
            }
            else
            {
                throw new ArgumentException("Blank instruction at " + pos, input);
            }
        }
        pos++;
    }

}
0
source
private static void CreateListString(string s)
{
string[] splits = s.Split(new char[] { ',' });
List<string> strs = new List<string>();
bool isLimiterSeen = false;
StringBuilder str = null;
for (int i = 0; i < splits.Length; i++)
{
if (splits[i].Contains("#("))
{
isLimiterSeen = true;
str = new StringBuilder();
}
if (!isLimiterSeen)
strs.Add(splits[i]);
else
{
str = str.Append("," + splits[i]);
if (splits[i].EndsWith(")"))
{
if (str.ToString().StartsWith(","))
strs.Add(str.ToString().Substring(1));
else
strs.Add(str.ToString());
isLimiterSeen = false;
str = null;
}
}
}
}
0
source

All Articles