0

I want to replace each char after 'to' with * using regex in Java.

Input:

String str = "thisisstringtoreplace"

Expected output:

thisisstringto*******

I am using

Pattern.compile("(?<=password=).*$")

This pattern replace all char with 1 * , I want * of remaining sting size (7 in this case). The action I want to perform is part of framework so I just need a regex for this.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Vishal Tank
  • 73
  • 1
  • 6

1 Answers1

0

You may use

s = s.replaceAll("(?<=\\G(?!^)|to).", "*");

See the regex demo.

Details

  • (?<=\G(?!^)|to) - either the end of the previous successful match or to
  • . - any char but a line break char.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397