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
  • 607,720
  • 39
  • 448
  • 563
Vishal Tank
  • 153
  • 2
  • 7

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
  • 607,720
  • 39
  • 448
  • 563