Java: matching with Regex and replacing each character
I have a line like:
There exists a word *random*.
random
will be a random word.
How can I use regex to replace each character random
with *
and have this result:
There exists a word ********.
So, *
replaces each character, in this case 6 characters.
Please note that I will only replace the word random
, not the environment *
. So far I have:
str.replaceAll("(\\*)[^.]*(\\*)", "\\*");
But it replaces *random*
with *
instead of what you want ********
(8 in total).
Any help really appreciated ...
source to share
If you only have one word: -
Regarding the current example, if you only have one word, then you can save yourself from regex using some 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();
If you can have multiple words: -
For this, here's a regex (this is where it starts to get a little complicated): -
String str = "There exists a word *random*.";
str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
System.out.println(str);
The above pattern replaces all characters not fully followed string containing even numbers of *
, with *
.
Depending on what suits you, you can use.
I will add an explanation for 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 only match words that inside a pair of stars
. If you don't have an imbalance stars
.
source to share
You can extract the word in between *
and make 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 this on ideone: http://ideone.com/VZ7uMT
source to share
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);
}
source to share