6
I have the following date/time format:
25/01/2017às11:53:37
And the following regex:
REGX_DATAHORA_DISTRIBUICAO =
"(?<data>\d{1,2}\/\d{1,2}\/\d{4})|(?<hora>\d{1,2}:\d{1,2}:\d{1,2})"
private OffsetDateTime getDataDistribuicao() {
String textoData = replaceAndTrim(this.getPaginaInfoGerais().<HtmlTableCell>getFirstByXPath(XPATH_CEL_DATA_DISTRIBUICAO)
.getTextContent());
return LocalDateTime
.parse(getDataDistribuicao(textoData),
DateTimeFormatter.ofPattern(PATTERN_DATA_HORA))
.atOffset(ZoneOffset.UTC);
}
private String getDataDistribuicao(final String dataTexto) {
final Matcher matcherDataHora = REGX_DATAHORA_DISTRIBUICAO.matcher(dataTexto);
if (matcherDataHora.find()) {
return matcherDataHora.group();
} else {
throw new RegexException("Data distribuição", REGX_DATAHORA_MOVIMENTACAO.pattern(), dataTexto);
}
}
The regex has 2 groups, but only one group is returned, the one of the date.. The other group of time returns as null.
I imagine it’s on the operator’s account...
I’ve tried using (?=
(positive lookahead
), but maybe I used it wrong.
What to do?
which post regex output format you want to get?
– Paz
an Offsetdatetime, I added the other method used.
– laaf
https://ideone.com/S81daa
– Guilherme Lautert
The reason is simple, you’re giving
return
, before going through all the groups.if (matcherDataHora.find()) { return matcherDataHora.group(); }
, ie you just checked whether you gavematch
and gavereturn
in the first group.– Guilherme Lautert
@Guilhermelautert yes, this Return failed to concatenate. But even so, the group 2 if you validate, is null. This same situation I found in the gringo forum. Even returning the two concatenated groups, the 2 is null in the same way.
– laaf