3

たとえば、文字列があります。

There exists a word *random*.

randomランダムな単語になります。正規表現を使用してのすべての文字をに置き換えて、次の結果を
得るにはどうすればよいですか?random*

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

したがって、*すべての文字、この場合は6文字が置き換えられます。周囲ではなく
、単語のみを置き換えていることに注意してください。これまでのところ:random*

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

ただし、目的の(合計8)ではなく、に置き換え*random*られます。 どんな助けでも、本当に感謝しています...*********

4

4 に答える 4

5

そのような単語が1つしかない場合:-

現在の例に関する限り、そのような単語が1つしかない場合は、いくつかの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

于 2013-02-04T06:14:27.313 に答える
2

間の単語を抽出し、その上で*すべての文字を置き換えることができ*ます。

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

于 2013-02-04T06:17:56.830 に答える
0
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);

}
于 2013-02-04T06:32:19.360 に答える
0

試す

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

出力

There exists a word ********.
于 2013-02-04T06:30:22.367 に答える