简体   繁体   English

搜索字符串中特殊字符之间的子字符串

[英]Search for a substring between special character in a string

I have a string like 我有一个字符串

(&(objectclass=${abc})(uid=${xyz}))

How can I search ${abc} and ${xyz} and replace with another strings. 如何搜索$ {abc}$ {xyz}并替换为另一个字符串。 There can be any number of occurence of such substrings ie${abc} in main string. 主字符串中可以出现任意数量的此类子字符串,即$ {abc}。 I was thinking of string.indexOf( but that could be messy. Any best approach? 我当时在想string.indexOf(但这可能是一团糟。任何最佳方法?

${abc} will be replaced by another string abc. $ {abc}将被另一个字符串abc替换。 I will get them from some other parameters. 我将从其他一些参数中获取它们。

You need to escape special characters ( $ , { , } ). 您需要转义特殊字符( ${} )。 Try this: 尝试这个:

str.replaceAll("\\$\\{abc\\}","abc");

This can be weird but can help you out: 这可能很奇怪,但可以帮助您:

str = str.substring(str.indexOf("${")+2, str.indexOf("}"))

This will work for all (any number of) strings between ${ and } . 这适用于${}之间的所有(任意数量)字符串。

I would use String.indexOf(String sep, int start) in a loop repeatedly to copy text and values to a StringBuilder. 我会在循环中反复使用String.indexOf(String sep, int start)将文本和值复制到StringBuilder。


Something like 就像是

public static void main(String... args) throws IOException {
    Map<String, String> map = new LinkedHashMap<>();
    map.put("abc", "ABC");
    map.put("xyz", "XYZ");
    printSubstitue("nothing to change", map);
    printSubstitue("(&(objectclass=${abc})(uid=${xyz})(cn=${cn})(special='${'))", map);
}

private static void printSubstitue(String text, Map<String, String> map) {
    String text2 = subtitue(text, map);
    System.out.println("substitue( "+text+", "+map+" ) = "+text2);
}

public static String subtitue(String template, Map<String, String> map) {
    StringBuilder sb = new StringBuilder();
    int prev = 0;
    for (int start, end; (start = template.indexOf("${", prev)) > 0; prev = end + 1) {
        sb.append(template.substring(prev, start));
        end = template.indexOf('}', start + 2);
        if (end < 0) {
            prev = start;
            break;
        }
        String key = template.substring(start + 2, end);
        String value = map.get(key);
        if (value == null) {
            sb.append(template.substring(start, end + 1));
        } else {
            sb.append(value);
        }

    }
    sb.append(template.substring(prev));
    return sb.toString();
}

prints 版画

substitue( nothing to change, {abc=ABC, xyz=XYZ} ) = nothing to change
substitue( (&(objectclass=${abc})(uid=${xyz})(cn=${cn})(special='${')), {abc=ABC, xyz=XYZ} ) 
     = (&(objectclass=ABC)(uid=XYZ)(cn=${cn})(special='${'))

It gets messy if you want to support nested ${ } values. 如果您想支持嵌套的${ }值,则会变得混乱。 ;) ;)

abc123=ghi123
abc${xyz}=def${xyz}

(&(objectclass=${abc${xyz}})(uid=${xyz}))

I would suggest you keep it simple if you can. 如果可以的话,我建议您保持简单。 ;) ;)

The regex you need is \\$\\{[^}]+\\} . 您需要的正则表达式为\\$\\{[^}]+\\} Replace with whatever is necessary. 替换为必要的东西。

edit: OK, I misunderstood the question. 编辑:好的,我误解了这个问题。 Regex fixed. 正则表达式固定。

A regex like this works: 像这样的正则表达式有效:

String regex = "(\\$\\{YOUR_TOKEN_HERE\\})";

Here is an example of usage: 以下是一个用法示例:

public static void main(String[] args) throws Exception {
    String[] lines = {
        "(&(objectclass=${abc})(uid=${xyz}))",
        "(&(objectclass=${abc})(uid=${xyz})(xyz=${xyz})(abc=${abc}))"
    };

    String token = "abc"; // or whatever

    String regex = String.format("(\\$\\{%s\\})", token);
    Pattern p = Pattern.compile(regex);
    for (String s: lines) {
        Matcher m = p.matcher(s);
        if (m.find()) {
            System.err.println(m.replaceAll("###"));
        }
    }
}

Output for token = "abc" is: 令牌=“ abc”的输出是:

(&(objectclass=###)(uid=${xyz}))
(&(objectclass=###)(uid=${xyz})(xyz=${xyz})(abc=###))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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