2

I have a string, which contains dates in yyyy-MM-dd format along with other symbols. Some of these dates contains the 'Z' symbol at the end.

For example:

control1970-04-26Z_shifted1982-02-01_Zulu1999-09-09Z

I need to eliminate these 'Z' symbols in the string and I guess regex would be a nice solution.

I've tried the following regex:

\d{4}-\d{2}-\d{2}Z{1}

But it matches the whole date, include 'Z' character.

Is there any way to match only 'Z' character and eliminate it using replaceAll method?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Alex Sergeenko
  • 642
  • 5
  • 22
  • 1
    Note that the Z stems from **zoned date time** where `Z` stands for zero, UTC, Greenwich mean time, +0:00. If you receive a Z, also +3:00 or -6:00 might be expected. [`LocalDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE))`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) would give such a date. In a pattern `X` would match the zone offset with a possible 'Z'. – Joop Eggen Oct 01 '18 at 09:16

2 Answers2

4

You may use a capturing group and replace with the placeholder to the value stored in this group:

String result = s.replaceAll("(\\d{4}-\\d{2}-\\d{2})Z", "$1");

See the regex demo. Here, (\\d{4}-\\d{2}-\\d{2}) is a capturing group due to the pair of unescaped parentheses, its value is stored in Group 1. Z is just matched. When we replace the match with $1, the date is pasted back into the resulting string and Z is discarded.

Note that if you want to make sure you only match four digits that are not preceded with another digit, prepend the pattern with (?<!\\d).

If you want to make sure there are no other letters after Z, you may append (?![a-zA-Z]) at the end of the pattern.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

To replace part of a string based on what comes before it, use a positive lookbehind:

str = str.replaceAll("(?<=\\d{4}-\\d{2}-\\d{2})Z", "");

See more on lookarounds here.

MikeM
  • 13,156
  • 2
  • 34
  • 47