Home > other >  regex match and replcae <..> characters
regex match and replcae <..> characters

Time:12-01

I have a requirement to match all array<..> in the entire sentence and replace only <> to [] (replace <> with [] which have prefix array).

I haven't got any clue to resolve this. It will be great if anyone can provide any clue for this issue?

Input

<tr><td>Asdft array<object> tesnp array<int></td>
<td>asldhj
ashd
repl array<String>
array
asdhl
afe array<object>
endoftest</td></tr>

Expected Output

<tr><td>Asdft array[object] tesnp array[int]</td>
<td>asldhj
ashd
repl array[String]
array
asdhl
afe array[object]
endoftest</tr></td>

CodePudding user response:

Use the regex, array<(\w )> to match the word characters within array< and > as group 1 and replace the angle brackets with square brackets keeping array and group 1 unchanged.

Demo:

public class Main {
    public static void main(String[] args) {
        String str = """
                <tr><td>Asdft array<object> tesnp array<int></td>
                <td>asldhj
                ashd
                repl array<String>
                array
                asdhl
                afe array<object>
                endoftest</td></tr>
                       """;

        String result = str.replaceAll("array<(\\w )>", "array[$1]");

        System.out.println(result);
    }
}

Output:

<tr><td>Asdft array[object] tesnp array[int]</td>
<td>asldhj
ashd
repl array[String]
array
asdhl
afe array[object]
endoftest</td></tr>

CodePudding user response:

You can use the method .replace(). This method search in a string for a specified character, and returns a new string where the specified character are replaced.

In your case you don't need a regex so you can just write:

String replacedStr = text.replace("array<object>", "array[object]");

CodePudding user response:

You can avoid using regexp here.

public static String replaceMacro(String str) {
    final String pattern = "array<";
    StringBuilder buf = new StringBuilder(str.length());
    int fromIndex = 0;

    while (true) {
        int lo = str.indexOf(pattern, fromIndex);

        if (lo >= 0) {
            lo  = pattern.length() - 1;
            int hi = str.indexOf('>', lo);

            buf.append(str, fromIndex, lo);
            buf.append('[');
            buf.append(str.substring(lo   1, hi));
            buf.append(']');

            fromIndex = hi   1;
        } else {
            buf.append(str.substring(fromIndex));
            break;
        }
    }

    return buf.toString();
}

In case you want to use regexp:

public static String replaceMacro(String str) {
    Pattern pattern = Pattern.compile("(array)<(\\w )>");
    Matcher matcher = pattern.matcher(str);

    StringBuilder buf = new StringBuilder(str.length());
    int fromIndex = 0;

    while (matcher.find(fromIndex)) {
        int lo = matcher.start();
        int hi = matcher.end();

        buf.append(str, fromIndex, lo).append(matcher.group(1));
        buf.append('[').append(matcher.group(2)).append(']');

        fromIndex = hi;
    }

    if (fromIndex < str.length()) {
        buf.append(str.substring(fromIndex));
    }

    return buf.toString();
}
  • Related