Find phone numbers within a txt file

1

How can I list .txt files that contain phone numbers in this format (99) 99999-9999 cell and (99 ) 9999-9999 fixed ?

This is the code that I have done so far, you just need to find the string in the format of a phone within the .txt file:

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;
            }
        }

Sample txt file, I want NOT to list file2.txt because it does not have a phone number:

Example of file1.txt:
55 (11) 97387-0245
ORGANIZATIONS LTD
55 (11) 99678-3199
55 (11) 97100-0445
MARIA JOSÉ DA SILVA at

Sample .txt file :
JOÃO DA SILVA
ORGANIZATIONS LTD
PADARIA ESTRELA
REMOTE 2 BROTHERS
MARIA JOSÉ DA SILVA at

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

    
asked by anonymous 25.07.2018 / 00:52

2 answers

2

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

link .

Testing Regex Online

link

In Pattern place:

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

In Input place:

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

Result

4 matches

Links on Regex .

link

link

link

Strongest documentation in English about Regex

link

Below your adjusted 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;
    }
}
    
25.07.2018 / 01:37
1

I think it works:

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

If you wanted to improve regex :

link

    
25.07.2018 / 01:52