简体   繁体   English

替换给定正则表达式java中没有的字符串

[英]Replace string not in given regex java

I try to replace string which not found in given regex. 我尝试替换在给定正则表达式中找不到的字符串。

Regex: (^.{1})|((.{1})(@.*))

Eg: If given input is dineshkani.n@gmail.com the above regex will extract "d", "n@gmail.com" . 例如:如果给定输入为dineshkani.n@gmail.com ,则上述正则表达式将提取"d", "n@gmail.com" I try to replace characters other than this regex find. 我尝试替换此正则表达式以外的字符。 I tried to get d**********n@gmail.com 我试图获得d**********n@gmail.com

Is there any way to replace like this. 有没有办法像这样替换。

Thanks in advance. 提前致谢。

Why don't you join those parts together? 您为什么不将这些部分结合在一起? In Java using join method: 在Java中,使用join方法:

String email = String.join("*****",arrayOfSplits);

Can you replace characters that exist between two anchor points using a regular expression? 您可以使用正则表达式替换两个锚点之间存在的字符吗? Yes. 是。

Take a look at the helpful resources below. 请看下面的有用资源。

Start of String and End of String Anchors 弦的开始和弦锚的结束

SO Question about obfuscating emails with REGEX patterns 所以关于用REGEX模式混淆电子邮件的问题

You are on the right path. 您走在正确的道路上。 Your pattern should contain at least 3 capture groups . 您的模式应至少包含3个捕获组 The first capture group will be the first character in the string, email address in your example. 第一个捕获组将是字符串中的第一个字符,即示例中的电子邮件地址。 The second capture group will be the characters from position 2 to the penultimate position before the @ symbol. 第二个捕获组是@符号前从位置2到倒数第二个位置的字符。 And the third capture group will be the rest of the characters. 而第三个捕获组将是其余角色。 Using your email as an example: 以您的电子邮件为例:

     [d]              [ineshkani.]       [n@gmail.com]
Capture group 1 --- Capture group 2 --- Capture group 3

Now that he groups are captured properly, we want to perform a replacement on capture group 2 that will replace each char with some obfuscation value, * for example. 现在已经正确捕获了他的组,我们想要对捕获组2进行替换,该替换将用一些混淆值(例如*)替换每个字符。 That will require a back reference to group 2. 这将需要向第二组回溯

([\w|\d]{1})  # This says match either a word or digit char occurring once
([\S]+)       # Match a non whitespace character with a greedy qualifier
(\S{1}\@\S+)  # Match a non whitespace character then an '@' then the rest of the characters until whitespace is found. 

Now you will need to use the backreference to group 2 in your Java replace function. 现在,您将需要在Java替换功能中使用向后引用到组2。 See this SO answer for a good example of ways to perform regex replace on capture groups. 请参阅此SO答案 ,以获取在捕获组上执行正则表达式替换的方法的良好示例。 You will need to use .replaceAll() and pass the capture group and the replacement string. 您将需要使用.replaceAll()并传递捕获组和替换字符串。 I hope this helps you solve your problem and understand what is going on when you use regular expressions to replace strings in Java. 我希望这可以帮助您解决问题并理解使用正则表达式替换Java中的字符串时发生的情况。

For more information on a greedy quantifier see the Regex Tutorial. 有关贪婪量词的更多信息,请参见Regex教程。

This is probably easier to not use regex at all. 根本不使用regex可能会更容易。

Why not just use a StringBuilder and just look from 1 to lastIndexOf("@") - 1 and set the char as * 为什么不只使用StringBuilder并从1 to lastIndexOf("@") - 1并将char设置为*

String email = "dineshkani.n@gmail.com";
StringBuilder sb = new StringBuilder(email);
int mp = sb.lastIndexOf("@");

for (int i = 1; i < mp - 1; i++) {
    sb.setCharAt(i, '*');
}

Output: 输出:

d**********n@gmail.com d**********n@gmail.com

Note : as mentioned in the comments, you'll have to check with this solution if the email is something like dn@gmail.com , it will just print that as is. 注意 :如评论中所述,您必须使用此解决方案检查电子邮件是否为dn@gmail.com类的dn@gmail.com ,它只会按原样打印。

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

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