Extract groups with regex

Asked

Viewed 404 times

2

I need to extract the Session and the date from the line below, and the date I have separated with dash and slash.

The Patterns work properly individually but when I try to extract both, nothing comes.

Pattern p = Pattern.compile("(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}) (\\d{2}/\\d{2}/\\d{4} | \\d{4}-\\d{2}-\\d{2})");
Matcher m = p.matcher("{E4AE5831-548B-4429-CB99-2429334A6348} | 16/03/2017 00:59:35 | [ColetaCPFVerificaColetaInicialReportCode] : [O seguinte prompt será vocalizad");

    if(m.find()){
        System.out.println(m.group(1));
        System.out.println(m.group(2));
    }
  • 3

    I edited your question by removing greetings as per this topic, to be in accordance with the website format

  • 1

    Thank you! ... I’ll pay more attention :)

  • If your or other answer was satisfactory, you can accept it as the answer to your question.

2 answers

1


The pattern should match all characters in the text.

  • Between the Session and the date there is no space, but "} | ".
  • The two date alternatives should not have spaces between them: (data1|data2).


Regular expression

^\{([\dA-F]{8}(?:-[\dA-F]{4}){4}[\dA-F]{8})} \| (\d{2}/\d{2}/\d{4}|\d{4}-\d{2}-\d{2})

Description

  • ^ - Circumflex that matches the beginning of the string.
  • \{ - Escape that houses a literal key "{".
  • ([\dA-F]{8}(?:-[\dA-F]{4}){4}[\dA-F]{8}) - Capture group (capturing group) that allows you to reference married text (using Matcher#group(int)) with:
    • [\dA-F]{8}(?:-[\dA-F]{4}){4}[\dA-F]{8} - The format of Session, allowing only hexadecimal characters. We use a no-capture group to save some characters.
  • } \| - House the literal "} | ".
  • (\d{2}/\d{2}/\d{4}|\d{4}-\d{2}-\d{2}) - Capture group with two alternatives:
    • Alternative 1: \d{2}/\d{2}/\d{4}.
    • Alternative 2: \d{4}-\d{2}-\d{2}.


Code

import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^\\{([\\dA-F]{8}(?:-[\\dA-F]{4}){4}[\\dA-F]{8})\\} \\| (\\d{2}/\\d{2}/\\d{4}|\\d{4}-\\d{2}-\\d{2})";
final String linha = "{E4AE5831-548B-4429-CB99-2429334A6348} | 16/03/2017 00:59:35 | [ColetaCPFVerificaColetaInicialReportCode] : [O seguinte prompt será vocalizad";

final Pattern p = Pattern.compile(regex);
final Matcher m = p.matcher(linha);

if(m.find()){
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

Upshot

E4AE5831-548B-4429-CB99-2429334A6348
16/03/2017

You can test here: http://ideone.com/qgRtt8

0

I was misinterpreting the way Pattern works.

The solution was to inform the characters between the two groups I wish to extract by inserting the quantifier

.*

Pattern p = Pattern.compile("(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}).*(\\d{2}/\\d{2}/\\d{4} | \\d{4}-\\d{2}-\\d{2})");
Matcher m = p.matcher("{E4AE5831-548B-4429-CB99-2429334A6348} | 16/03/2017 00:59:35 | [ColetaCPFVerificaColetaInicialReportCode] : [O seguinte prompt será vocalizad");
if(m.find()){
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}
  • May fail with {E4AE5831-548B-4429-CB99-2429334A6348} | 16/03/2017 00:59:35 | [Coleta] : [O seguinte 1111-22-33 casa a última data

Browser other questions tagged

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