[英]Java: Match with Regex and replace every character
I have a string, for ex: 我有一个字符串,例如:
There exists a word *random*.
random
will be a random word. random
将是一个随机的单词。
How can I use a regular expression to replace every character of random
with *
and have this result: 如何使用正则表达式替换
random
每个字符*
并得到以下结果:
There exists a word ********.
So the *
replaces every character, in this case 6 characters. 因此
*
替换每个字符,在这种情况下为6个字符。
Notice that I am after to replace only the word random
, not the surroundings *
. 请注意,我之后只更换
random
字,而不是周围环境*
。 So far I have: 到目前为止,我有:
str.replaceAll("(\\*)[^.]*(\\*)", "\\*");
But it replaces *random*
with *
, instead of the desired ********
(total of 8). 但它取代
*random*
与*
,而不是所期望的********
(总共8个)。
Any help, really appreciated... 任何帮助,真的很感激......
As far as current example is concerned, if you are having just a single word like that, then you can save yourself from regex, by using some String
class methods: - 就目前的例子而言,如果你只有一个这样的单词,那么你可以通过使用一些
String
类方法来保护自己免于正则表达式: -
String str = "There exists a word *random*.";
int index1 = str.indexOf("*");
int index2 = str.indexOf("*", index1 + 1);
int length = index2 - index1 - 1; // Get length of `random`
StringBuilder builder = new StringBuilder();
// Append part till start of "random"
builder.append(str.substring(0, index1 + 1));
// Append * of length "random".length()
for (int i = 0; i < length; i++) {
builder.append("*");
}
// Append part after "random"
builder.append(str.substring(index2));
str = builder.toString();
For that, here's a regex solution (This is where it starts getting a little complex): - 为此,这是一个正则表达式解决方案(这是它开始变得有点复杂的地方): -
String str = "There exists a word *random*.";
str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
System.out.println(str);
The above pattern replaces all the characters that is not followed by string containing even numbers of *
till the end, with a *
. 上述图案替换所有后面没有由字符
string containing even numbers of *
直到结束,用*
。
Whichever is appropriate for you, you can use. 无论哪种适合您,您都可以使用。
I'll add an explanation of the above regex: - 我将添加上述正则表达式的解释: -
(?<! ) // Not preceded by a space - To avoid replacing first `*`
. // Match any character
(?! // Not Followed by (Following pattern matches any string containing even number of stars. Hence negative look-ahead
[^*]* // 0 or more Non-Star character
[*] // A single `star`
[^*]* // 0 or more Non-star character
[*] // A single `star`
)* // 0 or more repetition of the previous pattern.
[^*]*$ // 0 or more non-star character till the end.
Now the above pattern will match only those words, which are inside a pair of stars
. 现在上面的模式只匹配那些
inside a pair of stars
单词。 Provided you don't have any unbalanced stars
. 只要你没有任何不平衡的
stars
。
You can extract the word between *
and do a replaceAll characters with *
on it. 您可以在
*
之间提取单词,并在其上执行替换所有字符*
。
import java.util.regex.*;
String txt = "There exists a word *random*.";
// extract the word
Matcher m = Pattern.compile("[*](.*?)[*]").matcher(txt);
if (m.find()) {
// group(0): *random*
// group(1): random
System.out.println("->> " + m.group(0));
txt = txt.replace(m.group(0), m.group(1).replaceAll(".", "*"));
}
System.out.println("-> " + txt);
You can see it on ideone: http://ideone.com/VZ7uMT 你可以在ideone上看到它: http ://ideone.com/VZ7uMT
try 尝试
String s = "There exists a word *random*.";
s = s.replaceAll("\\*.+\\*", s.replaceAll(".*(\\*.+\\*).*", "$1").replaceAll(".", "*"));
System.out.println(s);
output 产量
There exists a word ********.
public static void main(String[] args) {
String str = "There exists a word *random*.";
Pattern p = Pattern.compile("(\\*)[^.]*(\\*)");
java.util.regex.Matcher m = p.matcher(str);
String s = "";
if (m.find())
s = m.group();
int index = str.indexOf(s);
String copy = str;
str = str.substring(0, index);
for (int i = index; i < index + s.length(); i++) {
str = str + "*";
}
str = str + copy.substring(index + s.length(), copy.length());
System.out.println(str);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.