Get specific value in a txt file

Asked

Viewed 75 times

2

I am trying to get two specific values on each line of a txt file. As an example:

15/06/2016  R$ 638,69   R$ 2.562,46
15/07/2016  R$ 672,43   R$ 2.533,51
15/08/2016  R$ 658,93   R$ 2.504,75
15/09/2016  R$ 1.362,28   R$ 2.471,80
15/10/2016  R$ 1.342,20   R$ 2.435,41
15/11/2016  R$ 1.321,80   R$ 2.398,40

I need to get from each line the two values ex: "R$ 638,69" and "R$ 2,562,46", but as the values can change from cents to millions. There is no way I can read the string by position! I tried to mount a regexbut without success. Someone with a better idea?

    if (FileUpload1.HasFile)
                {
                    using (StreamReader texto = new StreamReader(FileUpload1.FileContent))
                    {
                        while ((mensagem = texto.ReadLine()) != null)
                        {
                            mensagemLinha.Add(mensagem);
                        }
                    }
    for (int i = 0; i < mensagemLinha.Count; i++)
                    {
                        string re1 = "()";  
                        string re2 = ".*?"; 
                        string re3 = "(\\$)";  
                        string re4 = "( )"; 
                        string re5 = ".*?"; 
                        string re6 = "(,)"; 
                        string re7 = ".*?"; 
                        string re8 = "(R)"; 
                        string re9 = "(\\$)";  
                        string re10 = "( )";    
                        string re11 = ".*?";    
                        string re12 = "(\\.)"; 
                        string re13 = ".*?";   
                        string re14 = "(,)";

                        Regex rex = new Regex(re1 + re2 + re3 + re4 + re5 + 
                        re6 + re7 + re8 + re9 + re10 + re11 + re12 + 
                        re13 + re14, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                        Match valores = rex.Match((mensagemLinha[i].ToString()));

                        lblArq.Text += valores ;
    }  
  • 3

    If the information is this take the given by the position he is in.

  • @Maniero the positions are not fixed, depending on the decimal places the blocking position is changed. Note that the position of the values of lines 4,5,6 were shifted.

2 answers

2


As Maniero said, "If the information is this take the given by the position he is in".

That is, by the example you have published, it is not necessary to use a regex.

using System;
using static System.Console;

class MainClass 
{
    public static void Main (string[] args) 
    {
        var linhasArquivo = new [] 
        { 
            "15/06/2016  R$ 638,69   R$ 2.562,46",
            "15/07/2016  R$ 672,43   R$ 2.533,51",
            "15/08/2016  R$ 658,93   R$ 2.504,75",
            "15/09/2016  R$ 1.362,28   R$ 2.471,80",
            "15/10/2016  R$ 1.342,20   R$ 2.435,41",
            "15/11/2016  R$ 1.321,80   R$ 2.398,40",
            "15/11/2016  R$ 789.785.987.498,40   R$ 287.654.987.498,40",
        };

        foreach(var linha in linhasArquivo)
        {
            var firstIndex = linha.IndexOf("R$") + 3; // +3 para contar o símbolo + espaço
            var lastIndex = linha.LastIndexOf("R$") + 3;

            var valor2 = linha.Substring(lastIndex);
            // Qualquer coisa a partir do último 'R$ '
            var valor1 = linha.Substring(firstIndex, linha.Length - firstIndex - valor2.Length - 3 - 3) ;
            // Qualquer coisa a partir do primeiro 'R$ ', até o próximo espaço

            WriteLine($"{valor1} - {valor2} ");
        }
    }
}

See working on Repl.it

  • 1

    If you don’t mind, I used the same data you mocked to make another example but with regex. :)

  • @Georgewurthmann Be my guest.

2

With regex could do so:

using System;
using System.Text.RegularExpressions;
using System.Linq;
using static System.Console;

class MainClass 
{
    public static void Main (string[] args) 
    {
        var linhasArquivo = new [] 
        { 
            "15/06/2016  R$ 638,69   R$ 2.562,46",
            "15/07/2016  R$ 672,43   R$ 2.533,51",
            "15/08/2016  R$ 658,93   R$ 2.504,75",
            "15/09/2016  R$ 1.362,28   R$ 2.471,80",
            "15/10/2016  R$ 1.342,20   R$ 2.435,41",
            "15/11/2016  R$ 1.321,80   R$ 2.398,40",
            "15/11/2016  R$ 789.785.987.498,40   R$ 287.654.987.498,40",
        };

        foreach(var linha in linhasArquivo)
        {
            MatchCollection matches = Regex.Matches(linha, @"(?:R\$\s?)(\d*\.*)*\d*,\d+");
            Console.WriteLine($"Achou {matches.Count} ocorrências...");

            var arrValues = matches.Cast<Match>()
                            .Select(m => m.Value)
                            .ToArray();
            //faça um foreach aqui se for necessário
            Console.WriteLine($"valor 1 = {arrValues[0]}");
            Console.WriteLine($"valor 2 = {arrValues[1]}");
        }
    }
}

Also I put in repl.it to see working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.