C # line splitting

Let's say I have this line:

"param1,r:1234,p:myparameters=1,2,3" 

... and I would like to break it into:

 param1 r:1234 p:myparameters=1,2,3 

I used the split function and, of course, split it into each comma. Is there a way to do this with regex, or will I have to write my own split function?

+4
source share
6 answers

Personally, I would try something like this:

 ,(?=[^,]+:.*?) 

Basically, use a positive look to find a comma followed by a key-value pair (this is determined by the key, colon and additional information [data] (including other commas). This should disqualify the commas between numbers, too.

+2
source

You can use ; to separate values, which simplifies working with it.

Since you have , for separation, as well as for values, it is difficult to break it down.

You have

 string str = "param1,r:1234,p:myparameters=1,2,3" 

Recommended use

 string str = "param1;r:1234;p:myparameters=1,2,3" 

which can be divided into

 var strArray = str.Split(';'); strArray[0]; // contains param1 strArray[1]; // r:1234 strArray[2]; // p:myparameters=1,2,3 
+2
source

Iโ€™m not sure how you write a split that knew which commas split there, honestly.

If this is not a fixed number every time in this case, just use String.Split overload, which takes an int , indicating how many substrings are returned at max.

If you have comma-delimited data that does not always contain a fixed number of elements and they can have literal commas in the data itself, it really should be indicated. If you can control the input in any way, you should encourage this and use the actual CSV parser instead of String.Split

+1
source

It depends. You cannot parse it with a regular expression (or anything else) unless you can define a consistent rule that divides one group into another. Based on your sample, I cannot clearly define such a rule (although I have some guesses). How does the system know that p:myparameters=1,2,3 is the only element? For example, if another element appears after it, what is the difference between this and 1,2,3 ? Set it out and you will be close to a solution.

If you can change the format of the input string, why not decide on a constant separator between your groups? ; would be a good choice. Use input like param1;r:1234;p:myparameters=1,2,3 , and there will be no ambiguity about where the groups are located, plus you can simply divide by ; and you donโ€™t need regex.

+1
source

The easiest approach is to change your delimiter to "," to something like "|". Then you can divide by "|" no problem. However, if you cannot change the delimiter, perhaps you can encode partitions in the same way as CSV.

CSV files have the same problem ... there are double quotes around the columns in the standard.

For example, your string would be "param1", "r: 1234", "p: myparameters = 1,2,3".

Then you can use Microsoft.VisualBasic.FileIO.TextFieldParser to split / parse. You can include this in C # even if it is in the VisualBasic namespace.

TextFieldParser

+1
source

You mean: string [] str = System.Text.RegularExpression.Regex.Spilt ("param1, r: 1234, p: myparameters = 1,2,3", @ "\,");

0
source

Source: https://habr.com/ru/post/1343106/


All Articles