0

I have a date type string yyyy-MM-ddTHH:mm:ss (2018-04-18T00:00:00)

I want to match : and replace it with %3a match - replace it with %2d

this my Regexp DEMO: https://regex101.com/r/rqpmXr/11

my output 2018%3a04%3a18T00%3a00%3a00

my expected output need to be like that 2018%2d04%2d18T00%3a00%3a00

MokiNex
  • 857
  • 1
  • 8
  • 21
  • 1
    See [Java URL encoding of query string parameters](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters). Edit: Also, Alex is right, you may just use `s.replace("-", "%2d").replace(":", "%3a")`. This makes more sense since Java regex does not support conditional replacement patterns (like in Boost or PCRE2). – Wiktor Stribiżew Apr 18 '18 at 12:12
  • 3
    URLEncoder.encode() will do the %3a but leave the `-` which is ok if this is an attempt at url encoding. If its not, why not just use a regular string replace. – Alex K. Apr 18 '18 at 12:12

1 Answers1

3
String date = "2018-04-18T00:00:00";
date = date.replace("-","%2d").replace(":","%3a");

I think it's much easier to use String.replace() method for your case.

Danila Zharenkov
  • 1,720
  • 1
  • 15
  • 27