Parsing strings in C #

I am new to C # and have a custom input line (really hope.)

This line will contain several sections and subsections of information.

For example:

1-7 will //represent values 1 2 3 4 5 6 7

3:.25:7 //will be the numbers contained between 3 and 7 in increments of .25 
        // (3 3.25 3.5 3.75 4 ... 6.75 7)
1,4,5  //will represent values 1 4 5

I would like to be able to iterate over a single line like this, and get as many arrays as there is data, separated by a semicolon (;)

The main goal is to parse something like this:

1-7;3:.25:10;1,5,9;4-7

and since there are 4 “data sets” in the above example, I have to create 4 new arrays containing the values. If I had n "datasets", I should create n new arrays.

Later, I would like to iterate over arrays in a nested manner using all combinations.

Also, if this is possible (not necessary), you can make some kind of mixing of views, as shown below:

1-7,9,16:2:20;

, , , .

!

+3
8

, , RegEx, string.Split().

';'

string[] datasets = inputString.Split(';');

, ',' , Split(';', ',')

string[] parts = datasets[i].Split(',');

: , .

string.IndexOf() /

string[] rangeParts = parts[j].Split('-');
string[] steppedParts = parts[j].Split(':');

2 3 .

TryParse(), - :

bool valid = double.TryParse(parts[k], 
  System.Globalization.NumberStyles.AllowDecimalPoint, 
  System.Globalization.CultureInfo.InvariantCulture, out value);

, .

+3

. .

^((?<section>[^;]+)(;|$))+

.

^((?<subsection>[^,]+)(,|$))+

.

(?<value>^[0-9]+$)|
(?<range>^[0-9]+-[0-9]+$)|
(?<rangewithstep>^[0-9]+:\.[0-9]+:[0-9]+$)

, .

^(?<start>[0-9]+)-(?<end>[0-9]+)$

^(?<start>[0-9]+):(?<step>\.[0-9]+):(?<end>[0-9]+)$

.


, . - , , -. , , , .

using System;
using System.Text.RegularExpressions;
using System.Globalization;

namespace RangeParser
{
    class Program
    {
        static void Main(string[] args)
        {
            String input = "1-7,9,16:2:20;1-7; 3:.75 : 10;1,5,9;4-7";

            Match sections = (new Regex(@"^((?<section>[^;]+)(;|$))+")).Match(input.Replace(" ", ""));

            foreach (Capture section in sections.Groups["section"].Captures)
            {
                Console.Write("Section ");

                Match subsections = (new Regex(@"^((?<subsection>[^,]+)(,|$))+")).Match(section.Value);

                foreach (Capture subsection in subsections.Groups["subsection"].Captures)
                {
                    Match subsectionparts = (new Regex(@"^(?<start>[0-9]*\.?[0-9]+)(((:(?<step>[0-9]*\.?[0-9]+):)|-)(?<end>[0-9]*\.?[0-9]+))?$")).Match(subsection.Value);

                    if (subsectionparts.Groups["start"].Length > 0)
                    {
                        Decimal start = Decimal.Parse(subsectionparts.Groups["start"].Value, CultureInfo.InvariantCulture);
                        Decimal end = start;
                        Decimal step = 1;

                        if (subsectionparts.Groups["end"].Length > 0)
                        {
                            end = Decimal.Parse(subsectionparts.Groups["end"].Value, CultureInfo.InvariantCulture);

                            if (subsectionparts.Groups["step"].Length > 0)
                            {
                                step = Decimal.Parse(subsectionparts.Groups["step"].Value, CultureInfo.InvariantCulture);
                            }
                        }

                        Decimal current = start;

                        while (current <= end)
                        {
                            Console.Write(String.Format("{0} ", current));

                            current += step;
                        }
                    }
                }

                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}

UPDATE

, , "1.5: 0.2: 3.6".

?

, 2. , 0,1 , , 0.100000001490116119384765625.

Single x = 0.0F;

for (int i = 0; i < 8; i++)
{
   x += 0.1F;
}

Console.WriteLine(x);

0.8000001 8 . 1000 0.00095, 99.99905 100.0, 100958,3 100 000.

, 10 , 0,1.

+2

#, , :

    var results = ParseExpression("1-7;3:.25:10;1,5,9;4-7");

    private static List<List<float>> ParseExpression(string expression)
    {
        // "x-y" is the same as "x:1:y" so simplify the expression...
        expression = expression.Replace("-", ":1:");

        var results = new List<List<float>>();
        foreach (var part in expression.Split(';'))
            results.Add(ParseSubExpression(part));

        return results;
    }

    private static List<float> ParseSubExpression(string part)
    {
        var results = new List<float>();

        // If this is a set of numbers...
        if (part.IndexOf(',') != -1)
            // Then add each member of the set...
            foreach (string a in part.Split(','))
                results.AddRange(ParseSubExpression(a));
        // If this is a range that needs to be computed...
        else if (part.IndexOf(":") != -1)
        {
            // Parse out the range parameters...
            var parts = part.Split(':');
            var start = float.Parse(parts[0]);
            var increment = float.Parse(parts[1]);
            var end = float.Parse(parts[2]);

            // Evaluate the range...
            for (var i = start; i <= end; i += increment)
                results.Add(i);
        }
        else
            results.Add(float.Parse(part));

        return results;
    }
+2

regex .

, ? , . - .

.

. , ( ). String.Split() , , . . decimal , . , "1-2-3;", "1:.:" - , . [2] "1: 2", , . , , , . - danbruc

, Microsoft PEX David. David , List<List<Decimal>>, .

, PEX, 45 9 .

019.;,-:!

, () . 9, , 0 1 - PEX 1000 999. 0 1, , 0.000 [...] 001 . , , . 44 (44 - 28 29 ), , . , . , .

, . PEX . ( ), NullReferenceException PEX , "9999999999999999999999999999999999" Decimal.Parse() OverflowException. PEX . , "!; 9,;.;., 990:!!:, 900: 09" , FormatException. . , ".0" . , , Decimal.Parse() () , . . .

PEX . . . FormatException, PEX IndexOutOfRangeException.

FormatException:           "!,"
FormatException:           ","
FormatException:           "1,"
FormatException:           "!"
FormatException:           ";9"
FormatException:           "::"
FormatException:           "!.999009"
FormatException:           "!.0!99!9"
FormatException:           "0,9.90:!!,,,!,,,,,,!,,,0!!!9,!"
FormatException:           ""
FormatException:           "-99,9"
FormatException:           "1,9,,,!,,,,,,9,,,9,1,!9,,,,!,!"
FormatException:           "!:,"
FormatException:           "!9!:.!!,!!!."
FormatException:           "!:"
IndexOutOfRangeException:  "1:9"
FormatException:           "09..::!"
FormatException:           "9,0..:!.!,,,!,,,,,,!,,,!!-,!,!"
OverflowException:         "99999999999999999999999999999999999999999999"
FormatException:           "!."
FormatException:           "999909!!"
FormatException:           "-"
FormatException:           "9,9:9:999,,,9,,,,,,!,,,!9!!!,!"
FormatException:           "!9,"
FormatException:           "!.09!!0!"
FormatException:           "9-;"
FormatException:           ":"
FormatException:           "!.!9!9!!"
NullReferenceException:    null
FormatException:           ":,"
FormatException:           "!!"
FormatException:           "9;"

, . try/catch. , , . , , .

, . , , PEX , Decimal.Parse().

ParseExpression(string)            100,00%  10/10 blocks
ParseSubExpression(string)          96,15%  25/26 blocks

ParseExpressionRegex(string)        95,06%  77/81 blocks
ParseExpressionRegexSingle(string)  94,87%  74/78 blocks

- . , , , . - , , . .

+2

# , , .

.

[2,4] - numbers between 2 and 4
(0,7] - numbers between 0 and 7, but not including 0
+1
0

ok, , ( a)

.

, - for after.

: , , ++ + = ( ).

- : , , .

,

.

, ( ), . - openID.

: , , , ?

0

, . , .

, , :

  • , 42

  • , 1-7

  • , 1:.5:7

, , . , .

, , , .

:

double.TryParse . CultureInfo.InvariantCulture , .

List<double> . , ToArray , .

0
source

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


All Articles