简体   繁体   中英

regex match and replcae <..> characters

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>

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>

This can be accomplished fairly easily with a simple regular expression:

(?<=array)  // positive lookbehind (preceded by "array")
<           // opening angle bracket
(\w+)       // one or more word characters (matching group)
>           // closing angle bracket

Working example

import java.util.regex.*;

public class Example {
    public static String replace(String str, String pattern, String replacement) {
        return Pattern
            .compile(pattern, Pattern.MULTILINE)
            .matcher(str)
            .replaceAll(replacement);
    }
    
    public static String fixHtmlText(String htmlText) {
        return replace(htmlText, "(?<=array)<(\\w+)>", "[$1]");
    }
    
    public static void main(String[] args) {
        String htmlText = "<tr><td>Asdft array<object> tesnp array<int></td>\n"
                        + "<td>asldhj\n"
                        + "ashd\n"
                        + "repl array<String>\n"
                        + "array\n"
                        + "asdhl\n"
                        + "afe array<object>\n"
                        + "endoftest</td></tr>";
        
        System.out.println(fixHtmlText(htmlText));
    }
}

Output

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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