Find phone numbers inside a txt file

Asked

Viewed 693 times

1

How can I list files .txt which contains numbers of telephones in that format (99)99999-9999 cellular and (99)9999-9999 fixed ?

This is the code I’ve made so far, just need to find inside the file . txt the string in the format of a phone:

private static void ListarArquivos()
        {
            try
            {

                string[] _diretorios = Directory.GetDirectories(@"D:\Arquivos\");
                Console.WriteLine("LSITA DE DIRETÓRIOS: ");
                Console.WriteLine("");

                foreach (string dir in _diretorios)
                {
                    Console.WriteLine(dir);

                    string[] arrArquivos = Directory.GetFiles(dir, "*.txt");

                    for (int x=0; x < arrArquivos.Length; x++)
                    {
                        if (File.Exists(arrArquivos[x]))
                        {
                            using (StreamReader sr = new StreamReader(arrArquivos[x]))
                            {
                                string linha;
                                while ((linha = sr.ReadLine()) != null)
                                {

                                    Console.WriteLine(linha);
                                }
                            }

                        }
                        else
                        {
                            Console.WriteLine(" O arquivo " + arrArquivos[x] + "não foi localizado !");
                        }

                        Console.WriteLine("");
                    }
                }
                Console.ReadKey();

            }
            catch
            {
                throw;
            }
        }

Example txt file, I want that NAY list the arquivo2.txt Why don’t you have a phone number:

Example of file 1.txt:
55(11)97387-0245
ORGANIZATIONS LTDA
55(11)99678-3199
55(11)97100-0445
MARIA JOSÉ DA SILVA

Example of file 2.txt:
JOÃO DA SILVA
ORGANIZATIONS LTDA
STAR BAKERY
QUITANDA 2 BROTHERS
MARIA JOSÉ DA SILVA

Example of file 3.txt:
(11)5489-9873
ORGANIZATIONS LTDA
PADARA ESTRELA
(11)5489-5912
MARIA JOSÉ DA SILVA

  • you would have some lines from your file to show?

  • @Tiedt Tech edited the post and inserted example of txt files

  • worked my answer?

  • Good morning @Tiedt Tech worked yes. Thank you.

2 answers

2


To validate you can use Regex. I made a simple example in

https://dotnetfiddle.net/VAsL7a.

Testing Regex Online

http://regexstorm.net/tester

In Pattern place:

\(..\)[0-9]{3,5}-[0-9]{4}

In Input put:

55(11)97387-0245
55(11)7387-0245
ORGANIZAÇÕES LTDA
55(11)99678-3199
55(11)97100-0445
MARIA JOSÉ DA SILVA

Upshot

4 pouches

Links about Regex.

https://docs.microsoft.com/pt-br/dotnet/standard/base-types/regular-expressions

https://msdn.microsoft.com/pt-br/library/3y21t6y4(v=vs.110). aspx

http://aurelio.net/regex/guia/seja-ninja.html

Strongest documentation in Portuguese on Regex

http://piazinho.com.br/

Below your set code.

private static void ListarArquivos()
{
    try
    {
        string[] _diretorios = Directory.GetDirectories(@"D:\Arquivos\");
        Console.WriteLine("LSITA DE DIRETÓRIOS: ");
        Console.WriteLine("");

        Regex rgx = new Regex("\\(..\\)[0-9]{3,5}-[0-9]{4}");

        foreach (string dir in _diretorios)
        {
            Console.WriteLine(dir);

            string[] arrArquivos = Directory.GetFiles(dir, "*.txt");

            for (int x=0; x < arrArquivos.Length; x++)
            {
                if (File.Exists(arrArquivos[x]))
                {
                    using (StreamReader sr = new StreamReader(arrArquivos[x]))
                    {                       
                        var total = rgx.Matches(sr.ToString()).Count;
                        if (total > 0)
                        {
                            Console.WriteLine("Tem telefones");
                        }
                    }
                }
                else
                {
                    Console.WriteLine(" O arquivo " + arrArquivos[x] + "não foi localizado !");
                }

                Console.WriteLine("");
            }
        }
        Console.ReadKey();

    }
    catch
    {
        throw;
    }
}
  • Worked perfectly!

1

I think it’ll do:

using System;
using System.IO;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
    Regex regex = new Regex(@"(\([(\d))]{2}\)\s?[(\d)]{4,5}+\-+[(\d)]{4})");
    // formatos válidos 
    //(00)0000-0000 
    //(00)90000-0000
    //(00) 0000-0000
    //(00) 90000-0000
    using (StreamReader reader = new StreamReader(@"arquivo.txt"))
    {
        string linha;
        while ((linha = reader.ReadLine()) != null)
        {
            // testa cada linha
            Match match = regex.Match(linha);
            if (match.Success)
                {
                    // arquivo tem telefone.                       
                }
            else 
               {
                    // arquivo não tem telefone.                       
               }
           }
        }
    }
}

Regular-Expressions.info

case wanted to improve the regex:

https://regex101.com/r/7Bb73s/1

  • Thank you! Very useful.

Browser other questions tagged

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