简体   繁体   English

使用正则表达式JAVA用*替换特定单词后的每个字符

[英]Replace each char after specific word with * using regex JAVA

I want to replace each char after 'to' with * using regex in Java. 我想在Java中使用regex用*替换'to'之后的每个字符。

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). 此模式将所有char替换为1 * ,我希望*剩余字符串大小(在这种情况下为7)。 The action I want to perform is part of framework so I just need a regex for this. 我要执行的操作是框架的一部分,因此我只需要一个正则表达式即可。

You may use 您可以使用

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

See the regex demo . 参见regex演示

Details 细节

  • (?<=\\G(?!^)|to) - either the end of the previous successful match or to (?<=\\G(?!^)|to) -无论是前面的成功匹配的端部或to
  • . - any char but a line break char. -除换行符以外的任何字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM