繁体   English   中英

Java:与Regex匹配并替换每个字符

[英]Java: Match with Regex and replace every character

我有一个字符串,例如:

There exists a word *random*.

random将是一个随机的单词。
如何使用正则表达式替换random每个字符*并得到以下结果:

There exists a word ********.

因此*替换每个字符,在这种情况下为6个字符。
请注意,我之后只更换random字,而不是周围环境* 到目前为止,我有:

str.replaceAll("(\\*)[^.]*(\\*)", "\\*");

但它取代*random** ,而不是所期望的******** (总共8个)。
任何帮助,真的很感激......

如果你只有这样一个词: -

就目前的例子而言,如果你只有一个这样的单词,那么你可以通过使用一些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();

如果你可以有这样的多个单词: -

为此,这是一个正则表达式解决方案(这是它开始变得有点复杂的地方): -

String str = "There exists a word *random*.";
str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
System.out.println(str);

上述图案替换所有后面没有由字符string containing even numbers of *直到结束,用*

无论哪种适合您,您都可以使用。

我将添加上述正则表达式的解释: -

(?<! )       // 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.     

现在上面的模式只匹配那些inside a pair of stars单词。 只要你没有任何不平衡的stars

您可以在*之间提取单词,并在其上执行替换所有字符*

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);

你可以在ideone上看到它: http ://ideone.com/VZ7uMT

尝试

    String s = "There exists a word *random*.";
    s = s.replaceAll("\\*.+\\*", s.replaceAll(".*(\\*.+\\*).*", "$1").replaceAll(".", "*"));
    System.out.println(s);

产量

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.

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