Find patterns within a String

Asked

Viewed 275 times

-3

I’m having trouble finding two patterns inside a java string, given a string. For example:

String str = 021478345231402198408189472328090419790164437663021101996091789834401805198616773422110231220010017325545008091968040901876902511198100789089990090919991753135151210101987; 

I know that every 11 digits is a Cpf and every 8 digits after Cpf is a date of birth.

I need to settle this matter

  1. Given a sequential file, with fixed-size records, containing CPF numbers (11 bytes) followed by birth dates (8 bytes).

Identify:

How many records are there in the file? What is the position of the 3rd record? (Create a formula) List a record per line and separating the fields for ,

I can use Pattern to locate x numbers, but I didn’t understand to find x and y pattern.

tried to do using a substring(0,9)+","; but he changed the last number by ,.

  • 3

    This is a college exercise or something?

  • That, data structures, we needed a light, we were able to do some things, but not locate right as you ask. adding the , we managed to break it using the split();

  • Each line in the file has 19 characters?

  • 2

    Create a class that represents the record. 11+8=19. So, create a for that separates this string into parts every 19 characters. For each part of 19 characters, you separate the first 11 of the CPF from the last 8 of the date and use them to create an instance of your record and then produce a list of those records. Write over the method toString of this class by placing the result separated by a comma. Finally, you iterate the produced list and give a System.out.println in each item.

  • so, this is a single line a single Str, I need to break it and find Cpf and rg for example, 02147834523,14021984,

2 answers

0


Can be done by creating a List and a pojo class with two Strings Cpf and birth

//Variaveis preliminares
    List<Pessoa> todos = new ArrayList<Pessoa>();
    String str = "021478345231402198408189472328090419790164437663021101996091789834401805198616773422110231220010017325545008091968040901876902511198100789089990090919991753135151210101987";
    int ncaracteres = str.length();
    int npessoas = ncaracteres / 19;
    String cpf,nascimento;

    //Produz a separação dos termos em uma List do tipo Pessoa e suas variáveis cpf e nascimento
    for (int i=0;i<npessoas;i++){
        int c = i * 19;
        cpf=str.substring(c+0,c+11);
        nascimento=str.substring(c+11,c+19);
        todos.add(new Pessoa(cpf,nascimento));
        System.out.println("CPF: "+cpf +" , "+"NASCIMENTO: "+nascimento);
    }

    //Uso dos dados conforme desejado,
    //lembrando que a posição de .get(x) para obter a 3 posição insere 2 pois o 0 conta como primeiro item
    System.out.println("Número de registros: "+todos.size());
    System.out.println("Dados 3 registro: "+todos.get(2).getCpf() + " NASCIMENTO: "+todos.get(2).getNascimento() );

public class Pessoa{
    String cpf;
    String nascimento;
    public Pessoa(String cpf, String nascimento) {
        this.cpf = cpf;
        this.nascimento = nascimento;
    }
    public String getCpf() {
        return cpf;
    }
    public String getNascimento() {
        return nascimento;
    }
}

-3

Here is an example of how to separate the string into characters so you can do the exercise. With this example you can separate the dates as well.

//Nesse exemplo só vou dividir os números a cada 11 números
   String str = "021478345231402198408189472328090419790164437663021101996091789834401805198616773422110231220010017325545008091968040901876902511198100789089990090919991753135151210101987"; 
     int b=1;
     String g = "";
        for (int a = 0; a < str.length(); a++) {
            //aqui é pego cada caracter da String
            String c = String.valueOf( str.charAt(a));
            //concatena uma nova string com os dados achados
            g = g + c;
            b++;

            //a cada 11 caracters é feita a impressão                      
            if (b > 11) {
            //Caso você queira salvar os dados correspondentes a cpf ou data
            //Você pode criar uma lista e adicionar os dados ja concatenado
            System.out.println("Disão a cada 11 números " + g);

                //iniciar e continuar o próximo loop do for
                 b = 1;
                 g="";
            }
    }

Browser other questions tagged

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