Java 用星号替换所有匹配的图案。等于匹配字符串的长度
我需要编写一个模式搜索器,并用*替换匹配的模式,我能够做到这一点,但替换的星是固定大小的。我希望替换的星星与匹配的图案具有相同的长度。他们是否有任何优化的方法来做到这一点 如在eg- 你将被替换为*** 数据由**** 及 我们被** 发出去——Java 用星号替换所有匹配的图案。等于匹配字符串的长度,java,regex,string,replace,pattern-matching,Java,Regex,String,Replace,Pattern Matching,我需要编写一个模式搜索器,并用*替换匹配的模式,我能够做到这一点,但替换的星是固定大小的。我希望替换的星星与匹配的图案具有相同的长度。他们是否有任何优化的方法来做到这一点 如在eg- 你将被替换为*** 数据由**** 及 我们被** 发出去——Hai man****怎么样?请告诉我****已获取的,因为****对****非常重要。 我要——Hai-man***你好吗?给我****这是***获取的,因为****对于**非常重要。您可以使用以下方法:匹配您需要的内容并使用它修改匹配的子字符串(将其
Hai man****怎么样?请告诉我****已获取的,因为****对****非常重要。
我要——
Hai-man***你好吗?给我****这是***获取的,因为****对于**
非常重要。您可以使用以下方法:匹配您需要的内容并使用它修改匹配的子字符串(将其中的所有字符替换为您所说的固定屏蔽字符)
看
注意:如果字符串包含换行符,则必须将
while
块中的replaceAll
更改为.replaceAll(“(?)”,“*”)
,以将换行符替换为*
,以下代码可能很容易理解
final String REGEX = "data|you|Us";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
Matcher matcher = PATTERN.matcher(message);
String maskedMessage ="";
while (matcher.find()) {
String rs = matcher.group();
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < rs.length(); i++){
sb.append("*");
}
message = message.replace(rs, sb.toString());
matcher = PATTERN.matcher(message);
}
maskedMessage = message;
System.out.println(maskedMessage);
final String REGEX=“data | you | Us”;
最终模式=Pattern.compile(REGEX);
String message=“海漫你好吗?给我你拿到的数据,因为它对我们来说是非常重要的数据”;
Matcher Matcher=PATTERN.Matcher(消息);
字符串maskedMessage=“”;
while(matcher.find()){
字符串rs=matcher.group();
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
您也可以通过以下方式实现:
final String REGEX = "data|you|Us";
final String MASK = "*";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
StringBuilder messageBuilder = new StringBuilder(message);
Matcher matcher = PATTERN.matcher(message);
int start=0;
while (matcher.find(start)) {
messageBuilder.replace(matcher.start(), matcher.end(), new String(new char[matcher.end()-matcher.start()]).replace("\0", MASK));
start = matcher.end();
}
System.out.println(messageBuilder.toString());
希望这能有所帮助。你的面具实际上就是字符吗?或者,
***
是否意味着仅屏蔽前4个字符有限制?是的,我的屏蔽应该*字符。等于匹配图案的长度否前4个字符没有限制。我想用stars替换整个匹配图案这是否意味着您的掩码实际上是*
,还是用户定义的?还有,看一看。它是否按预期工作?不,它是*(固定的),没有使用遮罩,它是固定的。我想用字符串隐藏模式*
final String REGEX = "data|you|Us";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
Matcher matcher = PATTERN.matcher(message);
String maskedMessage ="";
while (matcher.find()) {
String rs = matcher.group();
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < rs.length(); i++){
sb.append("*");
}
message = message.replace(rs, sb.toString());
matcher = PATTERN.matcher(message);
}
maskedMessage = message;
System.out.println(maskedMessage);
final String REGEX = "data|you|Us";
final String MASK = "*";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
StringBuilder messageBuilder = new StringBuilder(message);
Matcher matcher = PATTERN.matcher(message);
int start=0;
while (matcher.find(start)) {
messageBuilder.replace(matcher.start(), matcher.end(), new String(new char[matcher.end()-matcher.start()]).replace("\0", MASK));
start = matcher.end();
}
System.out.println(messageBuilder.toString());